I have a dataframe that looks like:
col_1
0 A
1 A:C:D
2 A:B:C:D:E
3 B:D
I'm trying to count each ':' to get to:
col_1 count
0 A 0
1 A:C:D 2
2 A:B:C:D:E 4
3 B:D 1
I've tried applying a function to no avail:
def count_fx(df):
return df.str.contains(':').sum()
df['count'] = df['col_1'].apply(count_fx)
Also,
df['count'] = df['col_1'].apply(lambda x: (x.str.contains(':')).sum(), axis=1)