[英]pandas merging two dataframes based on index and columns
I have following two dataframes: 我有以下两个数据框:
The measurements: 尺寸:
And the corresponding sample descriptions: 以及相应的示例说明:
I managed to get the second dataframe into the following format: 我设法将第二个数据帧转换为以下格式:
I would like to add the first dataframe to the second and name it something like 'values'. 我想将第一个数据框添加到第二个数据框,并将其命名为“值”。 How can I do that?
我怎样才能做到这一点? I tried merge with different options but without success.
我尝试使用不同的选项合并,但没有成功。
I solved it: 我解决了:
instead of bringing df_1 in the same form as df, I tried the opposite way: 我没有以与df相同的形式带来df_1,而是尝试了相反的方法:
and viola, merge is working like a charm: 和中提琴一样,merge的工作就像一个魅力:
df.index.name = 'row'
df_melt=pd.melt(df.reset_index(),id_vars='row',var_name='column')
pd.merge(df1,df_melt,on=['row','column'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.