简体   繁体   English

按值排序 dataframe 返回“对于多索引,label 必须是一个元组,其中元素对应于每个级别。”

[英]Sort dataframe by value returns "For a multi-index, the label must be a tuple with elements corresponding to each level."

Objective: Based off dataframe with 5 columns, return dataframe with 3 columns including one which is the count and sort by largest count to smallest. Objective: 基于 dataframe 的 5 列,返回 dataframe 的 3 列,其中一列是计数,并按从最大到最小的计数排序。

What I have tried:我试过的:

df = df[['Country', 'Year','NumInstances']].groupby(['Country', 'Year']).agg(['count'])

df = df.sort_values(by='NumInstances', ascending=False)

print(df)

Error: ValueError: The column label 'NumInstances' is not unique.错误:ValueError:列 label 'NumInstances' 不是唯一的。 For a multi-index, the label must be a tuple with elements corresponding to each level.对于多索引,label 必须是一个元组,其元素对应于每个级别。

Before this gets mark as a duplicate, I have gone through all other suggested duplicates and it seems they all suggest using the same code as I have above.在这被标记为重复之前,我已经浏览了所有其他建议的重复,似乎它们都建议使用与我上面相同的代码。

Is there something small that I am doing that may be incorrect?我正在做的一些小事情可能不正确吗?

Thanks!谢谢!

I guess you need to remove multi-index -我想你需要删除多索引 -

Try this -尝试这个 -

df = df[['Country', 'Year','NumInstances']].groupby(['Country', 'Year']).agg(['count']).reset_index()

or -或者 -

df = df[['Country', 'Year','NumInstances']].groupby(['Country', 'Year'], as_index=False).agg(['count'])

Found the issue.发现问题。 Adding an agg to the NumInstances column made the NumInstances column name a tuple of ('NumInstances', 'sum'), therefore I just updated the sort code to:向 NumInstances 列添加 agg 使 NumInstances 列名称成为 ('NumInstances', 'sum') 的元组,因此我刚刚将排序代码更新为:

df = df.sort_values(by=('NumInstances', 'sum'), ascending=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 label 'call_id' 列不是唯一的。 对于多索引,label 必须是一个元组,其元素对应于每个级别 - The column label 'call_id' is not unique. For a multi-index, the label must be a tuple with elements corresponding to each level 为多索引数据框的每个级别查找最小值 - Finding minimum value for each level of a multi-index dataframe 为多索引熊猫数据框中的每个值创建直方图 - Creating a histogram for each value in multi-index pandas dataframe 熊猫:通过索引级别0将多索引DataFrame与DataFrame一起分配 - Pandas: Assign multi-index DataFrame with with DataFrame by index-level-0 具有多索引的数据框过滤器:在给定值过滤器的情况下,返回顶级索引级别的所有行 - Dataframe filter with multi-index: return all rows at top index level given value filters Pandas - 如何将多索引数据框中的列缩放到每个级别= 0组的顶行 - Pandas - How to scale a column in a multi-index dataframe to the top row in each level=0 group 使用级别获取多索引Pandas DataFrame的最小索引 - Get index of the minimum of multi-index Pandas DataFrame using level 迭代多索引pandas中level = 1中的每个索引项 - iterate through each index item in level=1 in multi-index pandas Python Pandas 将 qcut 应用于多索引 dataframe 中的多索引级别 0 分组 - Python Pandas apply qcut to grouped by level 0 of multi-index in multi-index dataframe 仅保留 2 级多索引的一列值 Pandas dataframe 而忽略 NaN - Keeping only one column value of a 2 level Multi-index Pandas dataframe while ignoring NaN's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM