首页 > 解决方案 > Pandas 数据框列名似乎错误

问题描述

我是学生,因此是新手。我正在尝试按旧金山社区创建一个 Pandas 犯罪统计数据框。我的问题是我希望列名只是“邻居”和“计数”。相反,我似乎被一个单独的行卡住了,上面写着“('Neighborhood','count')”而不是正确的标签。这是代码:

df_counts = df_incidents.copy()
df_counts.rename(columns={'PdDistrict':'Neighborhood'}, inplace=True)
df_counts.drop(['IncidntNum', 'Category', 'Descript', 'DayOfWeek', 'Date', 'Time', 'Location', 'Resolution', 'Address', 'X', 'Y', 'PdId'], axis=1, inplace=True)
df_totals=df_counts.groupby(['Neighborhood']).agg({'Neighborhood':['count']})
df_totals.columns = list(map(str, df_totals.columns)) # Not sure if I need this
df_totals

输出:

('Neighborhood', 'count')
Neighborhood    
BAYVIEW     14303
CENTRAL     17666
INGLESIDE   11594
MISSION     19503
NORTHERN    20100
PARK        8699
RICHMOND    8922
SOUTHERN    28445
TARAVAL     11325
TENDERLOIN  9942

标签: python-3.xpandaspandas-groupby

解决方案


这里不需要agg(),你可以简单地做:

df_totals = df_counts.groupby(['Neighborhood']).count()
df_totals.columns = ['count']
df_totals = df_totals.reset_index() # flatten the column headers

如果你想打印没有数字索引的输出:

print(df_totals.to_string(index=False))

推荐阅读