Hi I have following data frame
S.No Description amount
1 a, b, c 100
2 a, c 50
3 b, c 80
4 b, d 90
5 a 150
I want to extract only the counts of a
expected answer:
description
a 3
CodePudding user response:
Use str.count
and sum
:
df['Description'].str.count('a').sum()
Output: 3
If you want all the counts:
df['Description'].str.split(', ').explode().value_counts()
Output:
a 3
b 3
c 3
d 1
Name: Description, dtype: int64
CodePudding user response:
Check split
with explode
then value_counts
s = df.Description.str.split(', ').explode().value_counts()
s['a']
Or we do
s = df.Description.str.split(', ',expand=True).eq('a').sum().sum()
CodePudding user response:
df.Description.str.count("a").sum()
Output: 3