[英]Merging two data frames based on the index column
I have two dataframes, I wanted to merge them into one single dataframe based on the matching row.我有两个数据帧,我想根据匹配的行将它们合并为一个数据帧。 My dataframe looks like this
我的数据框看起来像这样
DF_1 DF_1
Set_1 Fax_1 Fax_2 Abc_1 45 76 Abc_2 46 77 Abc_3 47 78 Abc_4 48 79 Abc_5 49 80 Abc_6 50 81 Abc_7 51 82 Abc_8 52 83 Abc_9 53 84 Abc_10 54 85
df_2 df_2
Set_1 Fax_3 Fax_4 Abc_1 69 42 Abc_2 70 43 Abc_3 71 44 Abc_6 72 45 Abc_5 73 46 Abc_6 74 47 Abc_7 75 48 Abc_8 76 49 Abc_9 77 50 Abc_10 78 51 Abc_11 55 86 Abc_12 56 87 Abc_13 57 88 Abc_14 58 89 Abc_15 59 90 Abc_16 60 91
The second one is a bigger dataframe and what I need as in my output file, as,第二个是一个更大的数据框,我在输出文件中需要什么,因为,
Set_1 Fax_1 Fax_2 Fax_3 Fax_4 Abc_1 45 76 69 42 Abc_2 46 77 70 43 Abc_3 47 78 71 44 Abc_4 48 79 72 45 Abc_5 49 80 73 46 Abc_6 50 81 74 47 Abc_7 51 82 75 48 Abc_8 52 83 76 49 Abc_9 53 84 77 50 Abc_10 54 85 78 51
This is what I tried with merge,这就是我尝试合并的方法,
merged =df.merge(df_annon, on='Set_1')
merged.head()
But it is just giving me the header as output.但它只是给我标题作为输出。 Any help or guidance is much appreciated..!!
非常感谢任何帮助或指导.. !!
I guess I wrote the answer in a comment already, but let me elaborate.我想我已经在评论中写了答案,但让我详细说明。
The pandas merge function takes keyword arguments: left_index=
and right_index=
. pandas 合并函数采用关键字参数:
left_index=
和right_index=
。 When set to True
, the merge function will use the index/indicies of the dataframe(s) for merging.当设置为
True
,合并函数将使用数据帧的索引/索引进行合并。
Like this:像这样:
merged = pd.merge(left=df, left_index=True
right=df_annon, right_index=True,
how='inner')
尝试这个
merged = df.merge(df_annon, left_index=True, right_index=True, how='inner')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.