专栏名称: 连享会
连玉君老师团队分享,主页:lianxh.cn。白话计量,代码实操;学术路上,与君同行。
目录
相关文章推荐
艾儿天空  ·  2月第3周起点月票榜:《Z时代艺术家》中断《 ... ·  18 小时前  
艾儿天空  ·  2025第8周:娘子护驾、大汉有活爹、恶龙等 ... ·  18 小时前  
古典文献学微刊  ·  编辑·手记 | ... ·  3 天前  
51好读  ›  专栏  ›  连享会

Stata-Python交互:在Stata中调用Python的三种方式

连享会  · 公众号  ·  · 2025-01-04 22:00

正文

👇 连享会 · 推文导航 | www.lianxh.cn

🍓 课程推荐: 连享会:2025 寒假班
嘉宾:连玉君(初级|高级);杨海生(前沿)
时间:2025 年 1 月 13-24 日
咨询:王老师 18903405450(微信)

作者: 李峥(厦门大学)
邮箱: [email protected]

致谢: 本文摘自以下文章,特此感谢!
Source: Chuck Huber, Stata/Python integration part 2: Three ways to use Python in Stata -Link-

温馨提示: 文中链接在微信中无法生效。请点击底部 「阅读原文」 。或直接长按/扫描如下二维码,直达原文:

Stata/Python 交互系列推文 源自 Stata 公司的统计项目总监 Chuck Huber 博士发表于 Stata 官网的系列博文,一共 9 篇。较为系统地介绍了 Stata 与 Python 的交互方式,包括:如何配置你的软件、如何实现 Stata 与 Python 数据集互通、如何调用 Python 工具包、如何进行机器学习分析等。

  • Part 1 : Setting up Stata to use Python -Link-
  • Part 2 : Three ways to use Python in Stata -Link-
  • Part 3 : How to install Python packages -Link-
  • Part 4 : How to use Python packages-Link-
  • Part 5 : Three-dimensional surface plots of marginal predictions-Link-
  • Part 6 : Working with APIs and JSON data -Link-
  • Part 7 : Machine learning with support vector machines, -Link-
  • Part 8 : Using the Stata Function Interface to copy data from Stata to Python, -Link-
  • Part 9 : Using the Stata Function Interface to copy data from Python to Stata, -Link-

中文编译稿列表如下:

  • Stata-Python交互-9:将python数据导入Stata
  • Stata-Python交互-8:将Stata数据导入Python
  • Stata-Python交互-7:在Stata中实现机器学习-支持向量机
  • Stata-Python交互-6:调用APIs和JSON数据
  • Stata-Python交互-5:边际效应三维立体图示
  • Stata-Python交互-4:如何调用Python宏包
  • Stata-Python交互-3:如何安装Python宏包
  • Stata-Python交互-2:在Stata中调用Python的三种方式
  • Stata-Python交互-1:二者配合的基本设定


目录

  • 1. 引言

  • 2. 使用方法

    • 2.1 以交互方式调用 Python

    • 2.2 do 和 ado 文档中运行 Python

    • 2.3 运行 Python 脚本

  • 3. 结语

  • 4. 相关推文



1. 引言

上一期文章介绍了如何下载安装 Python 并设置 Stata 来使用 Python。本期,我们要介绍在 Stata 中使用 Python 的三种方法:

  • 以交互方式调用 Python;
  • 在 do 和 ado 文档中运行 Python 代码;
  • 执行 Python 脚本文件。

2. 使用方法

2.1 以交互方式调用 Python

通过在 Stata 的命令窗口中输入 python ,实现 Stata 交互使用 Python。

. python
------------------------------ python (type end to exit) ------

Stata 提醒你完成后可以输入 end 退出 Python。现在,我们可以在命令窗口中键入 Python 代码。例如,可以指示 Python 打印短语。

>>> print("Hello Stata, I am Python")
Hello Stata, I am Python

当然,也可以使用 Python 作为交互式的计算器。

>>> 2*3
6
>>> 3*4
12

然后,输入 end 退出 Python 并返回到 Stata。

>>> end
---------------------------------------------------------------

当然,也可以简单地输入 python : 后跟 Python 语句。Python 将运行该语句,然后返回到 Stata。

. python: print("Hello Stata, I am Python")
Hello Stata, I am Python

还可以使用此语法进行快速计算.

. python: 2*3
6

2.2 do 和 ado 文档中运行 Python

在 do 和 ado 中可以使用 python: 后跟一个简单的命令,就可以实现交互方式中的效果,例如:

python: print("Hello Stata, I am Python")
Hello Stata, I am Python

还可以使用 Python 代码块在 do 和 ado 文档中运行多个 Python 语句。Python 代码块以 python 开头,以 end 结束,正如我们在上面的交互式示例中看到的。

在下例中,首先以 Stata 的 display 命令开头,然后使用 python 开始 Python 代码块,执行两个 Python 的 print() 语句,以 end 结束 Python 代码块,最后以 Stata 的 display 命令结束。

display "Hello Python, I am Stata."
python
print("Hello Stata.")
print("I am Python.")
end
display "Nice to meet you Python!"
. display "Hello Python, I am Stata."
Hello Python, I am Stata.

. python
------------------------------ python (type end to exit) ------
>>> print("Hello Stata.")
Hello Stata.
>>> print("I am Python.")
I am Python.
>>> end
---------------------------------------------------------------

. display "Nice to meet you Python!"
Nice to meet you Python!

需要注意的是,虽然 python python: 都可以运行代码块,但如果 Python 在代码块中遇到错误,二者处理方式有所不同。

如果使用 python 开始代码块,Python 遇到错误,将继续执行代码块中的其余代码。如果使用 python: 开始代码块,Python 遇到错误时,Python 将返回到 Stata,不会执行剩余的 Python 代码。无论哪种情况,都将打印堆栈跟踪并显示错误代码。

其次,Python 代码块中的缩进并不是为了美观而是功能性的。例如,可以编写 Python 代码块并缩进该块中的代码,如下例所示。

display "Hello Python, I am Stata."
python:
print("Hello Stata.")
print("I am Python.")
end
display "Nice to meet you Python!"
. display "Hello Python, I am Stata."
Hello Python, I am Stata.

. python:
------------------------------ python (type end to exit) ------
>>> print("Hello Stata.")
File "", line 1
print("Hello Stata.")
^
IndentationError: unexpected indent
(1 line skipped)
---------------------------------------------------------------
r(7102);

可以发现,上述语句报错,这告诉我们在 Stata 中调用 Python 语句不能随意缩进。关于 Python 中缩进的含义,详见「Python 文档」。

2.3 运行 Python 脚本

最后一种方式则是在 Stata 中运行 Python 脚本。Python 脚本是保存在具有 .py 扩展名的文件中的 Python 语句的集合。示例 3 显示了名为 hello.py 的 Python 脚本的内容。

*hello.py 地址:https://gitee.com/arlionn/data/blob/master/data01/hello.py
python script 地址\hello.py
Hello Stata.
I am Python.

当你使用他人编写的 Python 脚本时,这种在 Stata 中运行 Python 的方法非常有用。

3. 结语

以上就是在 Stata 中使用 Python 的三种方法。下期,将为大家介绍如何用 Stata 安装 Python 包。

4. 相关推文

Note:产生如下推文列表的命令为:
lianxh Stata Python +
安装最新版 lianxh 命令:
ssc install lianxh, replace

  • 专题:Stata入门
    • 使用 Jupyter Notebook 配置 Stata\Python\Julia\R
  • 专题:Stata程序
    • Stata程序:是否有类似-Python-中的-zip()-函数
  • 专题:文本分析-爬虫
    • VaR 风险价值: Stata 及 Python 实现
    • 支持向量机:Stata 和 Python 实现
  • 专题:Python-R-Matlab
    • Stata交互:Python-与-Stata-对比






请到「今天看啥」查看全文