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 elements in a row and create column counter in pandas

I have created the following pandas dataframe:

import pandas as pd

ds = {'col1' : ['A','A','B','C','C','D'],
      'col2' : ['A','B','C','D','D','A']}

df = pd.DataFrame(data=ds)

The dataframe looks like this:

print(df)

  col1 col2
0    A    A
1    A    B
2    B    C
3    C    D
4    C    D
5    D    A

The possible values in col1 and col2 are A, B, C and D.

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

I need to create 4 new columns, called:

  • countA: it counts how many A are in each row / record
  • countB: it counts how many B are in each row / record
  • countC: it counts how many C are in each row / record
  • countD: it counts how many D are in each row / record

So, from the example above, the resulting dataframe would look like this:

enter image description here

Can anyone help me please?

>Solution :

Probably this could work for you

from numpy import unique

for k in unique(df.values):
    df['count'+k] = df.eq(k).sum(axis=1)

which gives

  col1 col2  countA  countB  countC  countD
0    A    A       2       0       0       0
1    A    B       1       1       0       0
2    B    C       0       1       1       0
3    C    D       0       0       1       1
4    C    D       0       0       1       1
5    D    A       1       0       0       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