我正在做一个Coursera课程,想要使用for循环创建各种数据框,其想法是创建一个列表,然后将每个df添加到列表中。但是,以下返回错误:

File "<ipython-input-10-2863e455a5c5>", line 7
    array.append(county_df.where(county_df['STNAME']=state))
                                ^
SyntaxError: keyword can't be an expression




census_df = pd.read_csv('census.csv')
county_df=census_df[census_df['SUMLEV'] == 50]
county_df.head()
county_df['STNAME'].unique()
list = []
print type(list)
for state in county_df['STNAME'].unique():
    array.append(county_df.where(county_df['STNAME']=state))

print (list)

最佳答案

在大熊猫中,我们通常这样做。

l=[]

for _, df1 in county_df.groupby('STNAME'):
    l.append(df1)


您编码错误

county_df['STNAME']=state)

应该

county_df['STNAME']==state)

根据我的理解

county_df.loc[county_df['STNAME']==state,:]

关于python - 如何在 Pandas 中运行循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47128095/

10-09 16:45