Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame element-wise divide by sum of row inplace

Tags:

python

pandas

I want every element divided by sum of row inplace,code below always go 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
like image 691
F.bob Avatar asked May 29 '17 11:05

F.bob


People also ask

How to sum the rows and columns of a data frame?

Steps needed: 1 Create or import the data frame 2 Sum the rows: This can be done using the .sum () function and passing the parameter axis=1 3 Sum the columns: By using the .sum () function and passing the parameter axis=0 4 Filtering on the basis of required conditions

How do you find the sum of values in a Dataframe?

We can find the sum of each row in the DataFrame by using the following syntax: df. sum (axis=1) 0 128.0 1 112.0 2 113.0 3 118.0 4 132.0 5 126.0 6 100.0 7 109.0 8 120.0 9 117.0 dtype: float64. The output tells us: The sum of values in the first row is 128. The sum of values in the second row is 112. The sum of values in the third row is 113.

How to get row wise sum in R Dataframe using dplyr?

Other method to get the row sum in R is by using apply () function. row wise sum of the dataframe is also calculated using dplyr package. rowwise () function of dplyr package along with the sum function is used to calculate row wise sum. we will be looking at the following examples Row wise sum in R dataframe using rowSums ()

What is the sum of all rows for indexes 2 and 3?

On calculating the sum of all rows for these three columns, we find the sum to be zero for indexes 2 and 3. If you want to remove any of the columns from a list of columns that has sum equals to zero. We only sum those columns and apply the condition on them.


1 Answers

I think you need sum or maybe mean per rows (axis=1) with division by 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
like image 145
jezrael Avatar answered Oct 02 '22 14:10

jezrael