[英]Add column to a Python pandas DataFrame based on values in an other column
[英]python pandas - get piece of dataframe based on the values in column of other dataframe
假設我有df1
:
m n
0 a 1
1 b 2
2 c 3
3 d 4
和df2
:
n k
0 1 z
1 2 g
我只想獲取df1
的一部分,其中列'n'的值與df2
存在的df2
相同:
m n
0 a 1
1 b 2
最好的方法是什么? 事前看來微不足道,但令人驚訝的是,我嘗試過的任何方法都沒有。 例如我試過
df1[df1["n"] == df2["n"]]
但這給了我
ValueError: Can only compare identically-labeled Series objects
您在尋找isin
df1.loc[df1.n.isin(df2.n),:]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.