1

I have three dataframes:

df1, df2, df3.

Each of these dataframes has a variable (column1, column2, column3, respectfully) that has an id with it.

I have a master dataframe, called master_df, with column_master. This column, also, has an ID with it.

I would like to write a loop so that if column_master has any of the ids from df1, df2, or df3, create a new column called 'flag' and flag it: flag1 if the id was found in df1, flag2 if found in df2, flag3 if found in df3.

I attempted this so far, but I am at a loss as to how to finish the code:

def create_flag(df):

if df['column_master'] in df1['column1']:
    return df['flag']==flag_1  
elif df['column_master'] in ('column2'):
    return df['flag']==flag_2   
elif df['column_master'] in ('column3'):
    return df['flag']==flag_3 

    return df 

create_flag(master_df)

This throws off an error saying it does not recognize my column names. What am I doing wrong? and is there a better way to write this?

2 Answers 2

2

Avoid row-wise calculations with Pandas. You can use np.select with multiple conditions / values.

dfs = {1: df1, 2: df2, 3: df3}

conds = [df['column_master'].isin(dfx[f'column{idx}']) for idx, dfx in dfs.items()]
choices = [f'flag{i}' for i in range(1, len(conds)+1)]

df['flag'] = np.select(conds, choices, default='flag-None')
Sign up to request clarification or add additional context in comments.

Comments

0

In your code, I can see that for else condition you are missing the dataframe names

def create_flag(df):

if df['column_master'] in df1['column1']:
    return df['flag']==flag_1  
elif df['column_master'] in df2['column2']:
    return df['flag']==flag_2   
elif df['column_master'] in df3['column3']:
    return df['flag']==flag_3 

    return df 

create_flag(master_df)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.