I have two pandas.dataframes df1 and df2. Some of their index are equal. I want to find those index and combine the corresponding rows into a new dataframe.
df1 =
A B
Name
apple 1 5
orange 2 6
banana 3 7
df2 =
A B
Name
apple -1 10
audi -2 11
bmw 0 12
banana 2 8
vw -3 6
The new dataframe that I want is: 1) find the rows with the same index; 2) calculate the average value of the corresponding rows in column 'A'.
df_new =
A_average
Name
apple 0
banana 2.5
This is because: df1 and df2 both have the index apple and banana. The average value of apple in column 'A' is (1-1)/2=0, and the average value of banana in column 'A' is (3+2)/2=2.5.
Do you know how to use Python3 to achieve this? Please note that, in my real application, there can be many more rows than the example I showed above.
Thanks!