简体   繁体   中英

Calculate nonzeros percentage for specific columns and each row in Pandas

If I have a the following dataframe:

   df = pd.DataFrame({'name':['john','mary','peter','jeff','bill','lisa','jose'], 'gender':['M','F','M','M','M','F','M'],'state':['california','dc','california','dc','california','texas','texas'],'num_children':[2,0,0,3,2,1,4],'num_pets':[5,1,0,5,2,2,3]})

    name gender       state      num_children  num_pets
0   john      M  california             2         5
1   mary      F          dc             0         1
2  peter      M  california             0         0
3   jeff      M          dc             3         5
4   bill      M  california             2         2
5   lisa      F       texas             1         2
6   jose      M       texas             4         3

I want to create a new row and column pct. to get the percentage of zero values in columns num_children and num_pets Expected output:

    name gender       state      num_children  num_pets   pct.
0   pct.                              28.6%     14.3%     
1   john      M  california             2         5        0% 
2   mary      F          dc             0         1       50%
3  peter      M  california             0         0      100%
4   jeff      M          dc             3         5        0% 
5   bill      M  california             2         2        0%
6   lisa      F       texas             1         2        0%
7   jose      M       texas             4         3        0%

I have calculated percentage of zero in each row for targets columns:

df['pct'] = df[['num_children', 'num_pets']].astype(bool).sum(axis=1)/2
df['pct.'] = 1-df['pct']
del df['pct']
df['pct.'] = pd.Series(["{0:.0f}%".format(val * 100) for val in df['pct.']], index = df.index)

    name gender       state  num_children  num_pets  pct.
0   john      M  california             2         5    0%
1   mary      F          dc             0         1   50%
2  peter      M  california             0         0  100%
3   jeff      M          dc             3         5    0%
4   bill      M  california             2         2    0%
5   lisa      F       texas             1         2    0%
6   jose      M       texas             4         3    0%

But i don't know how to insert results below to row of pct . as expected output, please help me to get expected result in more pythonic way. Thanks.

df[['num_children', 'num_pets']].astype(bool).sum(axis=0)/len(df.num_children)
Out[153]: 
num_children    0.714286
num_pets        0.857143
dtype: float64

UPDATE: same thing but for calculation of sums, great thanks to @jezrael:

df['sums'] = df[['num_children', 'num_pets']].sum(axis=1)
df1 = (df[['num_children', 'num_pets']].sum()
                                       .to_frame()
                                       .T
                                       .assign(name='sums'))

df = pd.concat([df1.reindex(columns=df.columns, fill_value=''), df], 
                ignore_index=True, sort=False)
print (df)
    name gender       state  num_children  num_pets sums
0   sums                               12        18    
1   john      M  california             2         5   7
2   mary      F          dc             0         1   1
3  peter      M  california             0         0   0
4   jeff      M          dc             3         5   8
5   bill      M  california             2         2   4
6   lisa      F       texas             1         2   3
7   jose      M       texas             4         3   7

You can use mean with boolean mask by compare 0 values by DataFrame.eq , because sum/len=mean by definition, multiple by 100 and add percentage with apply :

s = df[['num_children', 'num_pets']].eq(0).mean(axis=1)
df['pct'] = s.mul(100).apply("{0:.0f}%".format)

For first row create new DataFrame with same columns like original and concat together:

df1 = (df[['num_children', 'num_pets']].eq(0)
                                       .mean()
                                       .mul(100)
                                       .apply("{0:.1f}%".format)
                                       .to_frame()
                                       .T
                                       .assign(name='pct.'))

df = pd.concat([df1.reindex(columns=df.columns, fill_value=''), df], 
                ignore_index=True, sort=False)
print (df)
    name gender       state num_children num_pets   pct
0   pct.                           28.6%    14.3%      
1   john      M  california            2        5    0%
2   mary      F          dc            0        1   50%
3  peter      M  california            0        0  100%
4   jeff      M          dc            3        5    0%
5   bill      M  california            2        2    0%
6   lisa      F       texas            1        2    0%
7   jose      M       texas            4        3    0%

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