简体   繁体   中英

count number of a specific string in entire data frame in Pandas and add its value in a new column

I have a 5 column data frame, I need to find how many times each element in first column(A) is repeated and add the amount in front of that element in a new column(F), for example 'a' in first Column(A) is repeated five times in entire data frame, so need to create column() and add 5 in related cell in row Zero and so on. appreciate to have your support. I am a newbie to python and need your precious comment.

below is the original data frame:

A   B   C   D   E
a   -            
b   a   -        
c   a   -        
d   b   a   -    
e   d   b   a   -

Preferred data frame would be:

A   B   C   D   E   F
a   -               5
b   a   -           3
c   a   -           1
d   b   a   -       2
e   d   b   a   -   1

So far I have coded below lines but I could not make new column having the sum.

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'},
 'B': {0: '-', 1: 'a', 2: 'a', 3: 'b', 4: 'd'}})

df['C'] = np.where(df['B'].isin(df['A'].values), df['B'], np.nan)
df['C'] = df['C'].map(dict(zip(df.A.values, df.B.values)))
df['D'] = np.where(df['C'].isin(df['B'].values), df['C'], np.nan)
df['D'] = df['D'].map(dict(zip(df.B.values, df['C'].values)))
df['E'] = np.where(df['D'].isin(df['C'].values), df['D'], np.nan)
df['E'] = df['E'].map(dict(zip(df['C'].values, df['D'].values)))

for cell in df['A']:
    print(cell)
    m=df.eq(cell).sum()
#   pd.DataFrame([m.values], columns=m.index)
    dep=sum(m)
    print(dep)
print(df) 

and below is the out put of above codes:

a 5 b 3 c 1 d 2 e 1

A   B   C   D   E
a   -            
b   a   -        
c   a   -        
d   b   a   -    
e   d   b   a   -

You can use Counter to create a mapping dictionary that contains unique elements in dataframe as its keys and their counts as values, then .map this dictionary on column A :

from collections import Counter

df['F'] = df['A'].map(Counter(np.ravel(df)))

   A  B  C  D  E  F
0  a  -           5
1  b  a  -        3
2  c  a  -        1
3  d  b  a  -     2
4  e  d  b  a  -  1
df['F'] = [np.count_nonzero(df.values == x) for x in df['A']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM