简体   繁体   English

在 Pandas value_counts() 中提取值

[英]Extract values in Pandas value_counts()

Say we have used pandas dataframe[column].value_counts() which outputs:假设我们使用了dataframe[column].value_counts()输出:

 apple   5 
 sausage 2
 banana  2
 cheese  1

How do you extract the values in the order same as shown above from max to min ?您如何按照与上述相同的顺序从 max 到 min 提取值?

eg: [apple,sausage,banana,cheese]例如: [apple,sausage,banana,cheese]

Try this:尝试这个:

dataframe[column].value_counts().index.tolist()
['apple', 'sausage', 'banana', 'cheese']
#!/usr/bin/env python

import pandas as pd

# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])

# What you're looking for
values = df['country'].value_counts().keys().tolist()
counts = df['country'].value_counts().tolist()

Now, print(df['country'].value_counts()) gives:现在, print(df['country'].value_counts())给出:

France       3
Germany      2
UK           1
Indonesia    1

and print(values) gives:print(values)给出:

['France', 'Germany', 'UK', 'Indonesia']

and print(counts) gives:print(counts)给出:

[3, 2, 1, 1]

如果有人在评论中错过了它,请尝试以下操作:

dataframe[column].value_counts().to_frame()

The best way to extract the values is to just do the following提取值的最佳方法是执行以下操作

json.loads(dataframe[column].value_counts().to_json())

This returns a dictionary which you can use like any other dict.这将返回一个字典,您可以像使用任何其他字典一样使用它。 Using values or keys.使用值或键。

 {"apple": 5, "sausage": 2, "banana": 2, "cheese": 1}

First you have to sort the dataframe by the count column max to min if it's not sorted that way already.首先你要sortdataframecountmaxmin ,如果没有排序已经如此。 In your post, it is in the right order already but I will sort it anyways:在您的帖子中,它的顺序已经正确,但无论如何我都会对其进行sort

dataframe.sort_index(by='count', ascending=[False])
    col     count
0   apple   5
1   sausage 2
2   banana  2
3   cheese  1 

Then you can output the col column to a list:然后您可以将col列输出到列表:

dataframe['col'].tolist()
['apple', 'sausage', 'banana', 'cheese']

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

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