我必须从CSV文件中获取每个州的县数,然后返回哪个州的县数最多。但是我不知道如何计算相同索引下的元素
我尝试使用.count()和numerate(),但返回一个错误键错误:“ Alabama”
这是我用来从CSV文件创建实际数据帧的代码
import pandas as PD
census = pd.read_csv('census.csv')
counties = pd.DataFrame({'State':census['STNAME'],'County':census['CTYNAME'],
'populations':census['CENSUS2010POP'],'population 2010': census['POPESTIMATE2010'],
'population 2015': census['POPESTIMATE2015']})
counties = counties.set_index(['State', 'County'])
答案 0 :(得分:0)
counties.groupby(['State','County']).agg('count')
counties = counties.set_index(['State', 'County']).count()