Pandas add column of count of another column across all the datafram

206 Views Asked by At

I have a dataframe:

df = C1  C2  E  
     1    2  3
     4    9  1
     3    1  1 
     8    2  8
     8    1  2

I want to add another columns that will have the count of the value that is in the columns 'E' in all the dataframe (in the column E) So here the output will be:

df = C1. C2. E. cou 
     1.   2. 3.  1 
     4.   9. 1.  2
     3.   1. 1   2
     8.   2. 8.  1
     8.   1. 2.  1 #2 appears only one it the column E

How can it be done efficiently ?

1

There are 1 best solutions below

6
Tim Roberts On

Here's one way. Find the matches and add them up.

import pandas as pd

data = [
    [1,2,3],[4,9,1],[3,1,1],[8,2,8]
]

df = pd.DataFrame( data, columns=['C1','C2','E'])
print(df)

def count(val):
    return (df['C1']==val).sum() + (df['C2']==val).sum()

df['cou'] = df.E.apply(count)
print(df)

Output:

   C1  C2  E
0   1   2  3
1   4   9  1
2   3   1  1
3   8   2  8
   C1  C2  E  cou
0   1   2  3    1
1   4   9  1    2
2   3   1  1    2
3   8   2  8    1