简体   繁体   中英

Compare Dataframes of different size and create a new one if condition is met

I need help to figure out the following problem:

I have two(2) dataframes of different sizes. I need to compare the values and, if the condition is met, replace the values in Dataframe 1.

If the values for a Material and Char in Dataframe 1 are = "Y", I need to get the "Required or Optional" value from Dataframe 2. If it's Required, then I replace the "Y" with "Y_REQD" otherwise if it's Optional then replace "Y" with "Y_OPT".

I've been using For loops but now the code is getting too complicated which hints me this may not be the best way.

Thanks in advance.

在此处输入图像描述

This is more like a pivot problem, then we can reindex the dataframe then sum

df1=df1.replace({'Y':'Y_'})+df2.pivot(*df2.columns).reindex_like(df1).fillna('')

Mostly agree with @WeNYoBen's answer. But to make it completely right, dataframe2 need to be revised using df.replace.

Short version:

df1=df1.replace({'Y':'Y_'})+df2.replace({'Rqd': 'REQD', 'Opt': 'OPT'}).pivot(*df2.columns).reindex_like(df1).fillna('')

Long version:

# break short into steps 
# 1. replace
df2 = df2.replace({'Rqd': 'REQD', 'Opt': 'OPT'})

# 2. pivot
df2 = df2.pivot(*df2.columns)

# 3. reindex
df2 = df2.reindex_like(df1)

# 4. fillna(cleanup df with string form)
df2 = df2.fillna('')

# 5. map on df1 and add up with df2
df1=df1.replace({'Y':'Y_'})+df2

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM