Reputation: 346
I have two dataframes:
df1=pd.DataFrame({'Product':("A","B","C"),"Data":(1,2,3)})
df2=pd.DataFrame({'Product':("A","B","C"),"Data":(2,4,6)})
I want to get the percentage variation numbers of the columns.
I have tried the below code:
data1_i=df1.set_index(["Product"])
data2_i=df2.set_index(["Product"])
data_diff=(data2_i-data1_i)/data1_i*100
data_diff.reset_index(inplace=True)
Output obtained:
Product Data
0 A 100.0
1 B 100.0
2 C 100.0
I am able to do this since the column name are same. How to get the variation if I have different column names like below:
df1=pd.DataFrame({'Product':("A","B","C"),"Data":(1,2,3)})
df2=pd.DataFrame({'Product':("A","B","C"),"Data_new":(2,4,6)})
I wont be able to use rename functions as I have lot of columns. I want to get the variation on numbers based on column positioning. Any pointers so that I can get started
Upvotes: 2
Views: 57
Reputation: 862911
You can select columns by position with iloc
:
df1=pd.DataFrame({'Product':("A","B","C"),"Data":(1,2,3)})
df2=pd.DataFrame({'Product':("A","B","C"),"Data_new":(2,4,6)})
data1_i=df1.set_index(["Product"])
data2_i=df2.set_index(["Product"])
data_diff=(data2_i.iloc[:, 0]-data1_i.iloc[:, 0])/data1_i.iloc[:, 0]*100
data_diff = data_diff.reset_index(name='Data')
print (data_diff)
Product Data
0 A 100.0
1 B 100.0
2 C 100.0
Upvotes: 1