Pandas - Counting Quantity Of Commas In Character Field
I have a pandas dataframe with a column filled with strings as is shown below: string_column 0 t,d,t,d,v,d 1 s,v,y,d 2 d,u,f I would like to create a new column with the cou
Solution 1:
Use str.count
:
df['comma_count'] = df.string_column.str.count(',')
print (df)
string_column comma_count
0 t,d,t,d,v,d 5
1 s,v,y,d 3
2 d,u,f 2
Solution 2:
use str.count
df.assign(comma_count=df.string_column.str.count(','))
string_column comma_count
0 t,d,t,d,v,d 51 s,v,y,d 32 d,u,f 2
Post a Comment for "Pandas - Counting Quantity Of Commas In Character Field"