简体   繁体   English

Pandas合并列,但不是“关键”列

[英]Pandas merge columns, but not the 'key' column

This may seem like a stupid question, but this has been bugging me for some time. 这似乎是一个愚蠢的问题,但这已经困扰了我一段时间。

df1: DF1:

imp_type    value
1           abc
2           def
3           ghi

df2: DF2:

id          value2
1           123
2           345
3           567

Merginge the 2 df's: Merginge 2 df:

df1.merge(df2, left_on='imp_type',right_on='id')

yields: 收益率:

imp_type    value    id    value2
1           abc      1     123
2           def      2     345
3           ghi      3     567

Then I need to drop the id column since it's essentially a duplicate of the imp_type column. 然后我需要删除id列,因为它基本上是imp_type列的副本。 Why does merge pull in the join key between the 2 dataframes by default? 为什么默认情况下合并会在两个数据帧之间拉入连接键? I would think there should at least be a param to set to False if you don't want to pull in the join key. 如果你不想拉入连接键,我认为至少应该设置为False的参数。 Is there something like this already or something I'm doing wrong? 有没有这样的东西或者我做错了什么?

I agree it would be nice if one of the columns were dropped. 我同意如果删除其中一个列会很好。 Of course, then there is the question of what to name the remaining column. 当然,还有一个问题是如何命名剩余的列。

Anyway, here is a workaround. 无论如何,这是一个解决方法。 Simply rename one of the columns so that the joined column(s) have the same name: 只需重命名其中一列,以便连接的列具有相同的名称:

In [23]: df1 = pd.DataFrame({'imp_type':[1,2,3], 'value':['abc','def','ghi']})

In [27]: df2 = pd.DataFrame({'id':[1,2,3], 'value2':[123,345,567]})

In [28]: df2.columns = ['imp_type','value2']

In [29]: df1.merge(df2, on='imp_type')
Out[29]: 
   imp_type value  value2
0         1   abc     123
1         2   def     345
2         3   ghi     567

Renaming the columns is a bit of a pain, especially (as DSM points out) compared to .drop('id', 1) . 重命名列有点痛苦,特别是(正如DSM指出的那样)与.drop('id', 1) However, if you can arrange for the joined columns to have the same name from the very beginning, then df1.merge(df2, on='imp_type') would be easiest. 但是,如果您可以从一开始就安排联接列具有相同的名称,那么df1.merge(df2, on='imp_type')将是最简单的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM