Reputation: 722
I have two dataframes that I need to get the means for plus merge based on their original column names. An example is this:
df = pd.DataFrame({
'sepal_length': [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0],
'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4],
'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5],
'petal_width': [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2]
})
df2 = pd.DataFrame({
'sepal_length': [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2],
'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4],
'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5],
'petal_width': [1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5]
})
I get the means like this:
df_one=df.mean(axis=0).to_frame('Mean_One')
df_two=df2.mean(axis=0).to_frame('Mean_Two')
The question is how to merge these two datagrams (df_one and df_two) since there is no column name for the original petal info (e.g., sepal_length, sepal_width, etc.). If there were I could do this
pd.merge(df_one, df_two, on='?')
Thanks for any help on this.
Upvotes: 1
Views: 38
Reputation: 384
If I'm understanding correctly you're trying to join the two averaged dataframes to have a column of average measurements for each of the dataframes.
If that's the case then you can join using their indexes:
pd.merge(df_one, df_two, left_index=True, right_index=True)
Output:
Mean_One Mean_Two
sepal_length 4.9125 0.2375
sepal_width 3.3875 3.3875
petal_length 1.4500 1.4500
petal_width 0.2375 1.4500
Upvotes: 2