简体   繁体   English

如何计算熊猫中每个唯一值的出现次数

[英]how to count occurrence of each unique value in pandas

I have large pandas dataframe, I would like to count the occurrence of each unique value in it, I try following but it takes to much time and memory usage.我有大熊猫数据框,我想计算其中每个唯一值的出现次数,我尝试遵循但需要花费大量时间和内存使用量。 How can I do it in a pythonic way?我怎样才能以pythonic的方式做到这一点?

pack=[]
for index,row in packets.iterrows ():
    pack.extend(pd.Series(row).dropna().values.tolist())

unique, count= np.unique(pack, return_counts=True)
counts= np.asarray((unique, count))

It seems like you want to compute value counts across all columns .似乎您想计算所有列的值计数。 You can flatten it to a series, drop NaNs, and call value_counts .您可以将其展平为一个系列,删除 NaN,然后​​调用value_counts Here's a sample -这是一个示例 -

df

     a    b
0  1.0  NaN
1  1.0  NaN
2  3.0  3.0
3  NaN  4.0
4  5.0  NaN
5  NaN  4.0
6  NaN  5.0
pd.Series(df.values.ravel()).dropna().value_counts()

5.0    2
4.0    2
3.0    2
1.0    2
dtype: int64

Another method is with np.unique -另一种方法是使用np.unique -

u, c = np.unique(pd.Series(df.values.ravel()).dropna().values, return_counts=True)
pd.Series(c, index=u)

1.0    2
3.0    2
4.0    2
5.0    2
dtype: int64

Note that the first method sorts results in descending order of counts, while the latter does not.请注意,第一种方法按计数降序对结果进行排序,而后者则不然。

暂无
暂无

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

相关问题 如何计算二维直方图中每个 bin 中每个唯一 ID 的出现次数(python 或 pandas) - How to count occurrence of each unique ID in each bin in a 2D histogram (python or pandas) 如何对 Pandas 中的列中的每个唯一值进行排序和计数 - How to sort and count each unique value in a column in Pandas 如何计算一列列表中每个唯一值的出现次数 Pandas - How to count occurrences of each unique value within a column of lists Pandas 计算单独列表中 Pandas 列中每个值的出现次数 - Count the occurrence of each value in a Pandas column in a separate list 如何计算 pandas 系列列表中每个元素的出现次数? - How to count occurrence of each element in pandas series of lists? 熊猫:通过循环遍历每列中的唯一值? - Pandas: count unique value in each column, by looping through them? 对于每个唯一的 Pandas 系列值,计算另一个字段 - For each unique Pandas series value, count an other field 如何转换pandas数据帧,使索引是唯一的值集合,数据是每个值的计数? - How to convert pandas dataframe so that index is the unique set of values and data is the count of each value? 计算一个值在多个列中的出现次数 dataframe Pandas - count occurrence of a value in multiple columns of a dataframe Pandas 在 Pandas 的每个组中,分组、过滤和计算特定值出现的有效方法是什么? - What's an efficient way to groupby, filter and count the occurrence of a particular value within each group in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM