Consider this dataframe:
pd.DataFrame(['A(3)BC(1)', 'A(2)BC(5)', 'A(1)BC(3)', 'A(2)BC(5)', 'A(4)BC(2)'], columns=['Column1'])
Column1
0 A(3)BC(1)
1 A(2)BC(5)
2 A(1)BC(3)
3 A(2)BC(5)
4 A(4)BC(2)
Is there a way to count the number of times A has a number higher than (3) without iterating through every line of the dataframe?
CodePudding user response:
Let's try
out = df['Column1'].str.extract('A\((\d )\)')[0].astype(int).gt(3).sum()
print(out)
1
CodePudding user response:
If the format is always A(X) at the start, you can do:
df['Column1'].apply(lambda st: int(st[st.find("A") 2:st.find(")")])).gt(3).sum()
Output
1