簡體   English   中英

python pandas-根據其他數據框的列中的值獲取數據框

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM