Python 在数据帧中对列进行分组和排序

Python 在数据帧中对列进行分组和排序,python,pandas,Python,Pandas,我有一个包含许多列的熊猫数据框,其中一列是“电影标题”,我想找到出现在最多行中的前5个电影标题,并按降序排列 例如: movie title Title 1 Title 2 Title 2 Title 3 Title 3 Title 3 应成为: movie title count Title 3 3 Title 2 2 Title 1 1 它可以在相同的数据帧中,也可以在新的数据帧中。我可能错过了一个简单的解决方案,因为我对熊猫非

我有一个包含许多列的熊猫数据框,其中一列是“电影标题”,我想找到出现在最多行中的前5个电影标题,并按降序排列

例如:

movie title

Title 1
Title 2
Title 2
Title 3
Title 3
Title 3
应成为:

movie title     count

Title 3         3
Title 2         2
Title 1         1
它可以在相同的数据帧中,也可以在新的数据帧中。我可能错过了一个简单的解决方案,因为我对熊猫非常陌生。谢谢你的帮助

试试看

df.groupby('movie title')['movie title'].aggregate(['count']).reset_index().sort('count', ascending=False)
或者一步一步地

df = df.groupby('movie title')['movie title'].aggregate(['count'])
df = df.reset_index()
df = df.sort('count', ascending=False)
聚合中的“[]”很重要

请尝试

df.groupby('movie title')['movie title'].aggregate(['count']).reset_index().sort('count', ascending=False)
或者一步一步地

df = df.groupby('movie title')['movie title'].aggregate(['count'])
df = df.reset_index()
df = df.sort('count', ascending=False)

聚合中的“[]”很重要

Get counts of the coulmn
df['movie title']。value_counts()
Get counts of the coulmn
df['movie title']。value_counts()