Pandas:无法进行位置索引

问题描述 投票:0回答:1

使用来自美国人口普查局的人口普查数据。县是美国各州的政治和地理分区。该数据集包含2010年至2015年美国各县和州的人口数据。

    cdf = pd.read_csv('census.csv')
    cdf = cdf[cdf['SUMLEV']==50]
    cdf.head()
    def answer_five():
        count = pd.DataFrame(cdf['STNAME'].unique(), columns =["State"])
        count['ct'] = 0
        i=0
        for item in cdf['STNAME']:
            if(item == count.iloc(i)['State']):
                count.iloc(i)['ct'] += 1
            else:
                i=i+1
        return count['State'] == count['State', np.max(count['ct'])]
    answer_five()

有人可以帮我找出for循环中嵌套的if else语句中的错误吗?我想不使用groupby来解决它

python pandas dataframe pandas-groupby series
1个回答
0
投票

pandas中的iloc函数使用括号来获取实际值。没有括号。应该是:

if(item == count.iloc[i]['State']):
            count.iloc[i]['ct'] += 1
© www.soinside.com 2019 - 2024. All rights reserved.