英文:
Pandas Mean and Merge Two DataFrames
问题
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 dataframes (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.
英文:
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.
答案1
得分: 2
如果我理解正确,您想要将这两个平均数据框连接起来,以获得每个数据框的平均测量列。
如果是这种情况,您可以使用它们的索引进行连接:
pd.merge(df_one, df_two, left_index=True, right_index=True)
输出:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论