Python 在 Pandas 的数据框中查找前 10 名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43859416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding top 10 in a dataframe in Pandas
提问by JD2775
I have a dataframe (df) with approx 800 rows with data like this:
我有一个包含大约 800 行数据的数据框 (df),如下所示:
Name:Jason
Age: 45
Ticket:1
姓名:Jason
年龄:45 门票:1
Name:Kim
Age: 30
Ticket:0
姓名:金
年龄:30 门票:0
1 = has a ticket 0 = does not have a ticket
1 = 有票 0 = 没有票
(sorry, that didn't format very well. It's basically 3 columns in the dataframe: Name, Age and Ticket)
(抱歉,格式不太好。它基本上是数据框中的 3 列:名称、年龄和票证)
Using Pandas, I am wondering what the syntax is for find the Top 10 oldest people who HAVE a ticket
使用 Pandas,我想知道查找有票的前 10 名最年长的人的语法是什么
So far I have:
到目前为止,我有:
df.sort_values('Age',ascending=False,inplace=True)(data.Ticket==1)
(data.head(10))
I know that's not correct but it shows what the parameters are that I am looking for. Any ideas? Thanks
我知道这不正确,但它显示了我正在寻找的参数。有任何想法吗?谢谢
回答by bigbounty
If you only want names of the old people,then
如果你只想要老人的名字,那么
df[df['Ticket'] == 1].sort_values('Age')['Names'].head(10)
回答by Teoretic
回答by piRSquared
mask, sort, head
面具, 排序, 头
df[df.Ticket == 1].sort_values('Age').head(10)