简体   繁体   English

如何根据一列中的相似值水平合并具有相同列名的两个数据框

[英]How to merge two data frames having same column names horizontally on basis of similar values in one column

I have two data frames as shown below:我有两个数据框,如下所示:

A一种 B C C D
Red红色的 36 36 1 1个 type-1类型 1
Blue蓝色的 78 78 2 2个 type-1类型 1
Green绿色的 59 59 3 3个 type-1类型 1
A一种 B C C D
Orange橙子 78 78 5 5个 type-2 2型
Purple紫色的 59 59 7 7 type-2 2型
Brown棕色的 36 36 9 9 type-2 2型

I want to merge the above two data frames on the basis of column B and after merge I want to keep the same columns as shown below:我想在 B 列的基础上合并上面的两个数据框,合并后我想保留相同的列,如下所示:

A一种 B C C D A一种 B C C D
Red红色的 36 36 1 1个 type-1类型 1 Brown棕色的 36 36 9 9 type-2 2型
Blue蓝色的 78 78 2 2个 type-1类型 1 Orange橙子 78 78 5 5个 type-2 2型
Green绿色的 59 59 3 3个 type-1类型 1 Purple紫色的 59 59 7 7 type-2 2型

Is it possible to do this using pandas or any other python function?是否可以使用 pandas 或任何其他 python 函数来执行此操作?

I have tried using pd.merge function but I needed to change the column names.我试过使用 pd.merge 函数,但我需要更改列名。 There exists another function called pd.concat but can I provide the column name (column 'B') in it for merging?存在另一个名为 pd.concat 的函数,但我可以在其中提供列名称(“B”列)以进行合并吗?

Thanks a lot in advance!非常感谢!

You can pass to parameters left_on and right_on columns from both DataFrames, so is created helper column key_0 , which is removed after join by DataFrame.merge :您可以将两个 DataFrame 的参数left_onright_on递给参数,因此创建了辅助列key_0 ,它在通过DataFrame.merge连接后被删除:

Notice: Pandas has problem with duplicated columns names, it is reason why merge rename them by suffix _x and _y注意:Pandas 有重复列名的问题,这就是merge后缀重命名它们的原因_x_y

df = df1.merge(df2, left_on=df1.B, right_on=df2.B).drop('key_0', axis=1)
print (df)
     A_x  B_x  C_x     D_x     A_y  B_y  C_y     D_y
0    Red   36    1  type-1   Brown   36    9  type-2
1   Blue   78    2  type-1  Orange   78    5  type-2
2  Green   59    3  type-1  Purple   59    7  type-2

What is problem with same columns names:相同列名有什么问题:

If need select column first A expected ouput is get Series.如果需要先选择A预期的输出是获取系列。

print (df.A_x)
0      Red
1     Blue
2    Green
Name: A_x, dtype: object

But if duplicated names get all columns in DataFrame, DONT DO IT :但是,如果重复的名称获得了DataFrame中的所有列,请不要这样做:

df = df.rename(columns=lambda x: x.split('_')[0])
# print (df)

print (df.A)
       A       A
0    Red   Brown
1   Blue  Orange
2  Green  Purple

apply rename to jezrael's anwer and you will get desired outputrename应用于 jezrael 的答案,您将获得所需的输出

out = (df1.merge(df2, left_on=df1.B, right_on=df2.B).drop('key_0', axis=1)
       .rename(columns=lambda x: x.split('_')[0]))

out

    A       B   C   D       A       B   C   D
0   Red     36  1   type-1  Brown   36  9   type-2
1   Blue    78  2   type-1  Orange  78  5   type-2
2   Green   59  3   type-1  Purple  59  7   type-2

it's really not a good idea to have duplicated column names, but we can use a multiindex, as for me it has more sence:使用重复的列名确实不是一个好主意,但我们可以使用多索引,因为对我来说它更有意义:

# initial column names after join
Index(['A_x', 'B_x', 'C_x', 'D_x', 'A_y', 'B_y', 'C_y', 'D_y'], dtype='object')

# convert to multiindex
d = df.columns.groupby(df.columns.str.extract('_(.+)')[0])
df.columns = pd.MultiIndex.from_tuples([(k,c.split('_')[0]) for k,v in d.items() for c in v])

# the result
       x                      y               
       A   B  C       D       A   B  C       D
0    Red  36  1  type-1   Brown  36  9  type-2
1   Blue  78  2  type-1  Orange  78  5  type-2
2  Green  59  3  type-1  Purple  59  7  type-2

暂无
暂无

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

相关问题 如何在具有相似值(但不相同)的公共列上合并 Pandas 中的两个数据框? - How do I merge two data frames in pandas on a common column which have similar values (but not the same)? 如何基于一个数据框中的一列和第二个数据框中的两列合并两个数据框 - How to merge two data frames based on one column in one data frame and two column in second dataframe 如果两个数据框中都存在列及其值,如何合并两个数据框? - How do I merge two data frames if a column and it's values exist in both data frames? 如何 select 数据框中的行在列值的基础上是相似的 - how to select rows in a data frame those are similar on basis of column values 连接两个数据帧,dataframe 中的一列具有多值数据 - Join two data frames, with one column in a dataframe having multivalue data 合并两个数据帧而不丢失列值 - Merge two data frames with out loosing column values pandas:如何在一列上合并具有相同列名的多个数据框? - pandas: How to merge multiple dataframes with same column names on one column? 如何在排除NaN值列的同时合并两个数据帧? - How to merge two data frames while excluding the NaN value column? 如何根据pandas python中的特定列合并两个数据框? - how to merge two data frames based on particular column in pandas python? 如何在 pandas 中连接两个具有不同列名的数据帧? - python - how to concat two data frames with different column names in pandas? - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM