我在一列中有两个重复的数据帧。我需要将副本提取到单独的数据帧中。
df1=
col1 col2 col3 col4
BLUE .5 yes 5
GREEN .2 no 2
PINK .3 yes 3
df2=
col1 col2 col3 col4
RED .9 yes 9
GREEN .2 yes 2
BLUE .7 yes 7
例如,尽管绿色行中的col2不一样,但我需要仅从df2中提取它,并将其拉入新的df。
而且还与成千上万的行一起工作,希望找到一种批量完成此操作的方法。
IIUC您只需使用。在
col1
上合并:
new_df = df2.merge(df1['col1'], on='col1')
输出:
col1 col2 col3 col4
0 GREEN 0.2 yes 2
1 BLUE 0.7 yes 7