【发布时间】:2021-03-19 19:08:00
【问题描述】:
您好,我有 2 个包含多行数据的数据框,但为简单起见,我只取出了一些数据框,例如:
df1:
id valid name note
------------------------------------------------------------------
1 yes tom, jane He is a engineer.She is a teacher.
1 no tim He's a doctor
2 no john He's a student
df2:
id name note Criterior1 Criterior2 valid
---------------------------------------------------------------------------------
1 tom He is a engineer. yes no no
1 jane She is a teacher. yes no no
1 tim He's a doctor. yes no yes
2 john He's a student no yes yes
df2 类似于 df1,但是,我将“note”和“name”列的单元格值组合在一起,它们共享相同的“id”和“valid”列值。
我想根据 id 将它们组合成一个数据框,从 df1 获取 id/valid/name/note 列,从 df2 获取 criterior1/criterior2 列,如下所示:
df3:
id valid name note Criterior1 Criterior2
---------------------------------------------------------------------------------------------
1 yes tom, jane He is a engineer.She is a teacher. yes no
1 no tim He's a doctor yes no
2 no john He's a student no yes
我尝试使用许多代码,例如:
df3=df2.merge(df1,how="left")
由于某种原因,我得到了 NaN 值,其中我组合了 id=1 和 valid = yes 等值。 但是对于我没有像 id=1 和 valid = no 这样合并的行,合并没有问题。
df3:
id valid name note Criterior1 Criterior2
---------------------------------------------------------------------------------------------
1 yes tom, jane He is a engineer.She is a teacher. NaN NaN
1 no tim He's a doctor yes no
2 no john He's a student no yes
【问题讨论】: