简体   繁体   English

如何计算特定名称出现在 Pandas 数据框列中的次数?

[英]How do I count the number of times a specific name appears in a pandas data frame column?

I have a column of names.我有一列名字。 I need to get a count of how many times a specific name appears in that column.我需要计算特定名称在该列中出现的次数。

Column:
Dave
John
John
Thanos
Bob

I need something like:我需要类似的东西:

[in] df['Column'].count_name('John')
[out] 2

Using value_counts() doesn't work because there are thousands of names in the column, and many of them only appear once.使用value_counts()不起作用,因为列中有数千个名称,其中许多只出现一次。 I'm sorry if this question has been asked/answered before, but I haven't been able to figure out a way to search for it that doesn't just give me an answer telling me to use value_counts() .如果之前有人问过/回答过这个问题,我很抱歉,但我一直无法找到一种搜索它的方法,而不仅仅是给我一个告诉我使用value_counts()的答案。

Thanks!谢谢!

Just for speed up using numpy.count_nonzero只是为了加速使用numpy.count_nonzero

import numpy as np 

np.count_nonzero(df['Column']=='John')
Out[186]: 2

Try using this:尝试使用这个:

df['Column:'].value_counts()['John']

In [6]: %timeit df[0].value_counts()['John']
1000 loops, best of 3: 548 µs per loop

In [7]: %timeit df[0].eq('John').sum()
The slowest run took 8.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 311 µs per loop

In [8]: %timeit np.count_nonzero(df[0]=='John')
10000 loops, best of 3: 162 µs per loop

EDIT: Fastest is using np.count_nonzero...编辑:最快的是使用 np.count_nonzero ...

Apparently, using eq() proves to be faster than using value_counts() which is obvious as value_counts calculates counts for all values whereas .eq() calculates for only given value..显然,使用 eq() 被证明比使用 value_counts() 更快,这很明显,因为 value_counts 计算所有值的计数,而 .eq() 仅计算给定值。

暂无
暂无

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

相关问题 如何计算列序列在数据框中出现的次数? - How to count the number of times a sequence of columns appears in a data frame? 计算字符串在特定列中出现的次数 - Count the number of times a string appears in a specific column 如何计算键的每个值出现的次数? - How do I count the number of times each value appears for a key? iPython:如何计算字符串在单元格中出现的次数? - iPython: how do I count the number of times a string appears in a cell? 如何在 pandas 数据帧的特定列索引处插入列? (更改 pandas 数据帧中的列顺序) - how do I insert a column at a specific column index in pandas data frame? (Change column order in pandas data frame) 计算pandas数据框中逐列的出现次数 - count occurrences of number by column in pandas data frame 如果列元素是一个集合,如何从 pandas 数据框列中获取每个值的计数? - How do I get count of each value from a pandas Data Frame column if the column elements is a set? 如何将列名放入pandas中具有特定条件的数据框单元格中 - how to put column name into data frame cell with specific conditions in pandas Python-如何在列数据框名称pandas中重置数字顺序? - Python - how to reset the order of number in the column data frame name pandas? 如何使用 pandas 根据日期列和天数列向数据框添加行 - How do I use pandas to add rows to a data frame based on a date column and number of days column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM