我有两个数据帧。
一个是这个:
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
答案 0 :(得分:1)
编辑:
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