![](/img/trans.png)
[英]Shift values in dataframe to the left if column name == Year and value is NaN pandas
[英]Pandas dataframe left join returning NaN when column value is different
嗨,所以我有 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”列值。
我想將它們組合成一個 dataframe 根據 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
嘗試這個:
df1.merge(df2, on='id', how='left')
如果在merge中沒有指定on
參數,默認情況下會根據所有常用列進行合並。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.