TypeError :("sort_values()对于参数'axis'具有多个值",“在索引SUMLEV处发生") [英] TypeError: ("sort_values() got multiple values for argument 'axis'", 'occurred at index SUMLEV')
本文介绍了TypeError :("sort_values()对于参数'axis'具有多个值",“在索引SUMLEV处发生")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么要输入此代码
cdf = census_df[census_df['SUMLEV'] == 50]
cdf = cdf.apply(lambda x:x.sort_values('CENSUS2010POP', axis=0, ascending=False)).reset_index(drop=True)
cdf = cdf.groupby('STNAME').head(3)
cdf.head(20)
出现以下错误
TypeError: ("sort_values() got multiple values for argument 'axis'", 'occurred at index SUMLEV')
虽然此代码可以正常工作
While this code works fine
cdf = census_df[census_df['SUMLEV'] == 50]
cdf = cdf.groupby('STNAME')
cdf = cdf.apply(lambda x:x.sort_values('CENSUS2010POP', axis=0, ascending=False)).reset_index(drop=True)
cdf = cdf.groupby('STNAME').head(3)
cdf.head(20)
但是在这里,我需要做两次groupby,首先在排序之前,然后在排序之后选择前3个值.我想先进行排序,然后分组,然后为每个分组选择3个.
But here I needed to do twice groupby, first before sorting and after sorting to pick top 3 values. I wanted first to sort, then group, then pick 3 for every group.
csv文件这里
谢谢!
推荐答案
您需要更改以下行:
cdf = cdf.apply(lambda x:x.sort_values('CENSUS2010POP', axis=0, ascending=False)).reset_index(drop=True)
收件人:
cdf = cdf.sort_values('CENSUS2010POP', ascending=False).reset_index(drop=True)
将.sort_values()
应用于数据框时,它将按您指定的列对所有列进行排序,即在这种情况下为CENSUS2010POP
.您不必将排序应用于所有列.
When .sort_values()
is applied to data frame, it will sort all columns by the column you specified, i.e. CENSUS2010POP
in this case. You don't have to apply the sort to all columns.
这篇关于TypeError :("sort_values()对于参数'axis'具有多个值",“在索引SUMLEV处发生")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文