我正在尝试使用PANDAS获取状态(' STNAME')中的最大城市数量(' CTYNAME') 我有一个数据框
df_filtered = census_df.copy().filter(items=['SUMLEV','STNAME','CTYNAME'])
df_filtered = df_filtered.set_index(['STNAME'])
state_df['STNAME'] = df.index.tolist()
state_df['STNAME'] = state_df['STNAME'].drop_duplicates()
state_df = state_df['STNAME'].dropna()
state_df = pd.DataFrame(state_df)
state_df.set_index(['STNAME'])
for state in state_df:
state_df['COUNT'] = df.loc[state].count()
出于某种原因,即使我将索引设置为州名(' STNAME'),索引也是一堆整数而不是州名(即阿拉斯加州,犹他州等)。
当我尝试运行代码时,它给了我一个关键错误
KeyError: 'the label [STNAME] is not in the [index]'
答案 0 :(得分:0)
执行for _ in df
时,实际上是在迭代标题。
尝试迭代系列state_df['STNAME']
for state in state_df['STNAME']:
# do stuff here with state
否则,使用state_df.iterrows()
迭代行。如果" STNAME"是你的索引,你可以这样做:
for row in state_df.iterrows():
state_name = row[0]
# do stuff here with state name
答案 1 :(得分:0)
您的索引未设置。试试这个。
编辑代码:
df_filtered = df_filtered.set_index('STNAME')