从第一个数据帧的两列中查找到第二个数据帧中的一列

2024-04-16 07:00:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧

其中之一是:

col1 col2 col3
43    21   2
32    31   4

第二

cl4 cl5 cl6
43   1  "text"
21   0  "text2"
32   1  "text3"

从数据帧1开始,col1和col2的值存在于第二个数据帧的cl4中

如何创建一个条件,从数据帧1中获取col1和col2的值,在数据帧2的cl4中查找,并基于此条件获取cl6的值

范例

df1.col1 == df2.cl4 && df1.col2 == df2.cl4

Tags: 数据text条件col2col3col1df1df2
1条回答
网友
1楼 · 发布于 2024-04-16 07:00:05

编辑:

print (df2)
   cl4  cl5    cl6
0   43    1   text
1   21    0  text2
2   31    1  text3
3   32    4  text4

#shift column for test next row
df2['a'] = df2['cl4'].shift(-1)
#join together next cl6 value
df2['new'] = df2['cl6'] + ', ' + df2['cl6'].shift(-1)
#remove last row of Dataframe because NaN
df2 = df2.iloc[:-1]

#create list of sets by actual and nex values
df2_sets = [set(x) for x in zip(df2['cl4'], df2['a'].astype(int))]
df1_sets = [set(x) for x in zip(df1['col1'], df1['col2'])]
#compare values and at least one True return True
#filter by boolena indexing
s = df2.loc[[any(x == y for y in df1_sets) for x in df2_sets], 'new']
print (s)
0     text, text2
2    text3, text4
Name: new, dtype: object

相关问题 更多 >