假设我们使用的熊猫dataframe[column].value_counts()
输出:
apple 5
sausage 2
banana 2
cheese 1
您如何从max到min以上面显示的相同顺序提取值?
例如: [apple,sausage,banana,cheese]
Answers:
尝试这个:
dataframe[column].value_counts().index.tolist()
['apple', 'sausage', 'banana', 'cheese']
dataframe[column].value_counts()
您会在底部看到dtype。有没有办法做到这一点?
dataframe[column].value_counts().to_frame()
.value_counts().index
以及.value_counts().values
#!/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()
现在,print(df['country'].value_counts())
给出:
France 3
Germany 2
UK 1
Indonesia 1
并print(values)
给出:
['France', 'Germany', 'UK', 'Indonesia']
并print(counts)
给出:
[3, 2, 1, 1]
dataframe.value_counts()
还是series.value_counts()
?您在哪种数据类型中输出?