简体   繁体   English

如何计算 dataframe 中的特定元素?

[英]How can I count a specific element in dataframe?

In a dataframe like below在 dataframe 中,如下所示

          equips
0       bags, balls 
1        glasses
2    shoes, caps, bags
3          NaN
4     spoons, balls

I want to count the numbers of comma and make another column like below我想计算逗号的数量并制作另一列,如下所示

          equips              the number of comma
0       bags, balls                    1
1        glasses                       0
2    shoes, caps, bags                 2
3          NaN                         0
4     spoons, balls                    1

Please, I need your help..!拜托我需要你的帮忙..!

You can use str.count method as follows:您可以使用str.count方法,如下所示:

df['the number of comma'] = df['equips'].str.count(',')

You can use simple list comprehension like so:您可以像这样使用简单的列表理解:

df['the number of comma'] = [i.count(',') for i in df['equips']]

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

相关问题 如何计算 dataframe 中特定间隔的元素? - How can I count the element in specific intervals in a dataframe? 在数据框中如何计数特定值,然后选择计数最高的值来创建另一个数据框? - In a dataframe how can I count a specific value and then select the value with the highest count to create another dataframe? Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 如何制作计数高于特定值的数据框? - How can i make a dataframe where the count is higher than a specific value? 如何计算一个元素在 dataframe 的两列中出现的次数? - How can I count the number of occurrences of an element across two columns of a dataframe? 我怎么能算出特定的二元词呢? - how can I count the specific bigram words? 如何在另一个 dataframe 的特定列名上填写 dataframe - How can I fill a dataframe on specific column names of another dataframe 如何计算列表列表中某个位置的特定元素的出现次数? - How do I count occurrences of a specific element in a position in a list of lists? 如何检查特定元素的复选框? - How can I check a checkbox for a specific element? 如何计算dataframe列中重复值的频率? - How can I count the frequency of repeated values in dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM