Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count ocurrencies of pattern in pandas dataframe based on condition

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Let’s try

out = df['Column1'].str.extract('A\((\d+)\)')[0].astype(int).gt(3).sum()
print(out)

1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading