DataFrame element separates by sum of rows inplace

I want each item to be divisible by the sum of the inplace string, the code below was always wrong.

pandas newbie, thanks!

df = pd.DataFrame(np.random.rand(12).reshape(3,4),columns=list('abcd'))
df_row_sum = df.apply(lambda x: x.mean(),axis=1)
df / df_row_sum

      

+3


source to share


2 answers


I think you need, sum

or maybe mean

for axis=1

division by strings ( ) DataFrame.div

:

np.random.seed(123)
df = pd.DataFrame(np.random.randint(10, size=12).reshape(3,4),columns=list('abcd'))
print (df)
   a  b  c  d
0  2  2  6  1
1  3  9  6  1
2  0  1  9  0

print (df.sum(axis=1))
0    11
1    19
2    10
dtype: int64

print (df.div(df.sum(axis=1), axis=0))
          a         b         c         d
0  0.181818  0.181818  0.545455  0.090909
1  0.157895  0.473684  0.315789  0.052632
2  0.000000  0.100000  0.900000  0.000000

      




print (df.mean(axis=1))
0    2.75
1    4.75
2    2.50
dtype: float64

print (df.div(df.mean(axis=1), axis=0))
          a         b         c         d
0  0.727273  0.727273  2.181818  0.363636
1  0.631579  1.894737  1.263158  0.210526
2  0.000000  0.400000  3.600000  0.000000

      

+7


source


Using @jezrael setting

np.random.seed(123)
df = pd.DataFrame(np.random.randint(10, size=12).reshape(3,4),columns=list('abcd'))
print (df)

   a  b  c  d
0  2  2  6  1
1  3  9  6  1
2  0  1  9  0

      



Use numpy

and restore a new data frame

v = df.values
pd.DataFrame(
    v / v.sum(1, keepdims=True),
    df.index, df.columns
)

          a         b         c         d
0  0.181818  0.181818  0.545455  0.090909
1  0.157895  0.473684  0.315789  0.052632
2  0.000000  0.100000  0.900000  0.000000

      

0


source







All Articles