繁体   English   中英

Python Pandas 使用[groupby]时的中间列排序

[英]Python Pandas sorting of the middle column when using [groupby]

I am using python pandas and would like to sort the output by the middle column of the below tables(i have shown the output I am getting and the desired output that i want to get)

I am using the groupby function within pandas to get the output however it is sorting by count column (see below output table), instead i want to sort by the YOB column (please see desired output table)

另外,我如何计算每个国家的平均出生年份。

import pandas as pd

xlpath= "C:/Users/Username/documents/Datafile.xlsx"

df = pd.read_excel(eval('xlpath'))

y = df.groupby('COUNTRY').YOB.value_counts(ascending=False)

print(y)



Output:

在此处输入图像描述

Desired Output:

在此处输入图像描述

期待您的反馈。

谢谢

假设您不关心“国家/地区”列的排序(因为您没有指定有问题的),这是实现每个国家/地区每年分组计数的一种方法,保持年份按升序排列:

df2 = df.groupby(["Country", "YOB"]).count()
df2 = df2.sort_values(["Country","YOB"], ascending=[True, True])
print(df2)

或者在一行中:

print(df.groupby(["Country", "YOB"]).count().sort_values(["Country","YOB"], ascending=[True, True]))

您可以尝试的一种方法是在应用 groupby 之前对 YOB 上的 dataframe 进行排序。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM