下面的第一段代码在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这意味着Python编码错误,但是由于相同的代码行在Python中运行时没有错误,所以Stata可能也有问题。
发布于 2022-03-05 23:01:20
df = Data.get('id bin v')将在列表列表中返回变量id、bin和v。ie df[0]保存id、bin和v的第一行值。
尝尝这个
* STATA CODE:
python
import pandas as pd
from sfi import Data
cut_bins = range(-5,4)
df = pd.DataFrame(Data.getAsDict('id bin v'))
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end现在,df.head()将为和 v):生产(使用我的假数据)。
id bin v cut_bins
0 1.0 1.0 2.535117 (2, 3]
1 2.0 2.0 0.161955 (0, 1]
2 3.0 3.0 -0.397220 (-1, 0]
3 4.0 4.0 0.003145 (0, 1]
4 5.0 5.0 -2.985772 (-3, -2]https://stackoverflow.com/questions/71334602
复制相似问题