Iva
Iva

Reputation: 45

divide different columns of dataframe python

I have a dataframe with multiple columns

     0    1    2    3
2    1    2    2  155
3    1   92    2  145
4    1   87    2  123
5    1   85    2  146
6    1   79    3  138

I want to crease a new df with the result of deviding col 0/col1, col2/col3...and so on Thanks for the help!

Upvotes: 0

Views: 81

Answers (2)

Pablo C
Pablo C

Reputation: 4761

You can do something like this:

df1 = df[df.columns[::2]]
df2 = df[df.columns[1::2]]
df2.columns = df1.columns
df1/df2
          0         2
2  0.500000  0.012903
3  0.010870  0.013793
4  0.011494  0.016260
5  0.011765  0.013699
6  0.012658  0.021739

Upvotes: 2

Grasshopper
Grasshopper

Reputation: 436

import pandas as pd

#original dataframe
org_df = pd.DataFrame({'0': [1,1,1,1,1], '1':[2,92, 87, 85, 79], '2':[2,2,2,2,2], '3': [155,145,123,146,138]}, index = [2,3,4,5,6])

df = pd.DataFrame()
df['col1'] = org_df['0']/org_df['1']
df['col2'] = org_df['2']/org_df['3']

print(df)

Upvotes: 0

Related Questions