[英]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.