Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

python - Compare data in df1 and df2 columns based on third df3 and get data for matched row data from df2 last column

I have 3 dataframes in df1

srno col1 col2 col3 col4
1 a1 a2 a3
2 b1 c2 c3
3 d1 b2
4 e1 e2 e3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
del df1['col4']

# read every condition in df3
# and merge related df2 columns to df1
# with the condition column

for i, row in df3.iterrows():
    priority = row['priority']
    col_list = row['col_combination'].split(',')
    df1 = pd.merge(df1, df2[col_list + ['col4']], on=col_list, how='left')
    # rename every merge df1's new col4 to priority no.
    df1.rename(columns={'col4':priority}, inplace=True)
    

print(df1)

#    srno col1 col2 col3    1    2
# 0     1   a1   a2   a3   g1  NaN
# 1     2   b1   c2   c3  NaN   g3
# 2     3   d1   b2  NaN  NaN   g2
# 3     4   e1   e2   e3  NaN  NaN

priority_list = df3['priority'].tolist()
obj = df1[priority_list[0]]
# use combine_first to merge priority(columns) 1, 2
for priority in priority_list[1:]:
    obj = obj.combine_first(df1[priority])

# re-assign
df1['col4'] = obj

print(df1)

#    srno col1 col2 col3    1    2 col4
# 0     1   a1   a2   a3   g1  NaN   g1
# 1     2   b1   c2   c3  NaN   g3   g3
# 2     3   d1   b2  NaN  NaN   g2   g2
# 3     4   e1   e2   e3  NaN  NaN  NaN


# finally del priority columns
df1.drop(priority_list, axis=1, inplace=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...