简体   繁体   中英

Dividing all elements in a single row of a DataFrame by an element in another column in that same row and then do this for all rows using pandas?

Say I have a simple dataframe as below and I want to take the ith element in column_4 and divide it into the ith elements in all of the other columns (except column_4 ) for that particular row and then perform this on all rows.

Another way of saying it is how do I normalize columns 1, 2, 3 by column 4? Is there an easy way of doing this?

Eg

Input

import pandas as pd

df = pd.DataFrame({'column_1':[1,2,3], 'column_2':[4,5,6], 'column_3':[7,8,9], 'column_4':[2,3,4]})
df.head()

Desired Output

df2 = pd.Dataframe({'column_1':[1/2,2/3,3/4], 'column_2':[4/2,5/3,6/4], 'column_3':[7/2,8/3,9/4], 'column_4':[2,3,4]})
df2.head()

Notice each element in column_1,column_2,column_3 has been divided by its equivalent element in column_4 .

Note: In this case I have used a relatively small DataFrame however was also wondering whether there is a way to generalize the result for a DataFrame that has say 1000's of rows and 100's of columns.

Option 1

You can simply do

df['column_1'] = df['column_1'] / df['column_4']

and so on

The operations are vectorized as in numpy

If you want a new dataframe do

In [18]: import pandas as pd
    ...: df = pd.DataFrame({'column_1':[1,2,3], 'column_2':[4,5,6], 'column_3':[7,8,9], 'column_4
    ...: ':[2,3,4]})
    ...: df.head()
Out[18]:
   column_1  column_2  column_3  column_4
0         1         4         7         2
1         2         5         8         3
2         3         6         9         4

In [19]: new_df = pd.DataFrame()

In [20]: for col in ['column_1', 'column_2', 'column_3']:
    ...:     new_df[col] = df[col] / df['column_4']
    ...:

In [21]: new_df
Out[21]:
   column_1  column_2  column_3
0  0.500000  2.000000  3.500000
1  0.666667  1.666667  2.666667
2  0.750000  1.500000  2.250000

Option 2

As pointed out by anky, you could solve this in a one liner like this:

df.drop('column_4',1).div(df['column_4'],axis=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