从 Stata 17 调用 Python 时出现问题
下面的第一个代码在 python 中运行没有问题,但是当在 Stata do 文件中使用相同的行时,会出现一条消息,提示 Python 编码错误。知道什么可能导致这个问题吗? (我在 Windows 上使用 Stata 17。)
# PYTHON CODE:
import pandas as pd
df = pd.read_stata('data.dta')
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df['cut_bins'] = pd.cut(df['v'], bins=cut_bins)
* STATA CODE:
python
import pandas as pd
from sfi import Data
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df = Data.get('id bin v')
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end
在 do 文件中运行 Stata 代码时,会出现以下消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
This suggest a Python编码错误,但由于相同的行在Python中运行没有错误,因此也可能存在是Stata的问题。
The first code below runs without problems in python, but when using the same lines in a Stata do-file, a message appears suggesting a Python coding error. Any idea what could cause this problem? (I am using Stata 17 on Windows.)
# PYTHON CODE:
import pandas as pd
df = pd.read_stata('data.dta')
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df['cut_bins'] = pd.cut(df['v'], bins=cut_bins)
* STATA CODE:
python
import pandas as pd
from sfi import Data
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df = Data.get('id bin v')
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end
When running the Stata code in a do-file, the following message appears:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
This suggests a Python coding error but since the identical lines run in Python without error, it may as well be that there is a problem with Stata.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
df = Data.get('id bin v')
将返回变量id
、bin
和v
在列表的列表中。即df[0]
保存id
、bin
和v
的第一行值。现在尝试一下
,
df.head()
将生成(使用我的id
、bin
和v< 的假数据/代码>
):
df = Data.get('id bin v')
will return the variablesid
,bin
, andv
in a list of lists. iedf[0]
hold the first row of values ofid
,bin
, andv
.Try this
Now,
df.head()
would produce (using my fake data forid
,bin
andv
):