Python 列熊猫中唯一值的计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41665659/
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
Count of unique value in column pandas
提问by kwashington122
I have a dataframe and I am looking at one column within the dataframe called names
我有一个数据框,我正在查看数据框中名为名称的一列
array(['Katherine', 'Robert', 'Anne', nan, 'Susan', 'other'], dtype=object)
I am trying to make a call to tell me how many times each of these unique names shows up in the column, for example if there are 223 instances of Katherine etc. How do i do this? i know value_counts just shows 1 for each of these because they are the separate unique values
我想打电话告诉我这些唯一名称中的每一个在列中出现多少次,例如,如果有 223 个 Katherine 等实例。我该怎么做?我知道 value_counts 只为每个显示 1,因为它们是单独的唯一值
回答by ade1e
If I understand you correctly, you can use pandas.Series.value_counts.
如果我理解正确,您可以使用pandas.Series.value_counts。
Example:
例子:
import pandas as pd
import numpy as np
s = pd.Series(['Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])
s.value_counts()
Katherine 1
Robert 1
other 1
Anne 1
Susan 1
dtype: int64
The data you provided only has one of each name - so here is an example with multiple 'Katherine' entries:
您提供的数据每个名称中只有一个 - 因此这里是一个包含多个“Katherine”条目的示例:
s = pd.Series(['Katherine','Katherine','Katherine','Katherine', 'Robert', 'Anne', np.nan, 'Susan', 'other'])
s.value_counts()
Katherine 4
Robert 1
other 1
Anne 1
Susan 1
dtype: int64
When applied to your Dataframeyou will call this as follows:
当应用于您的数据框时,您将按如下方式调用它:
df['names'].value_counts()
回答by Istvan
You could use group by to achieve that:
您可以使用 group by 来实现:
df[['col1']].groupby(['col1']).agg(['count'])