0

I have three dataframes:

array = {'name': ['Trevor', 'John', 'Trevor', 'John', 'Trevor', 'Trevor', 'John'], 'day': ['2020-10-11 08:00:00', '2020-10-11 08:00:00', '2020-10-11 08:00:00', '2020-10-11 08:00:00', '2020-10-11 08:00:00'
        , '2020-10-11 12:34:00', '2020-10-11 09:12:00'], 'number': [5,0,3,5,1,8,6]}
df = pd.DataFrame(array)
array1 = {'name': ['Trevor'], 'color': ['red']}
df1 = pd.DataFrame(array1)
array2 = {'name': ['John'], 'color': ['blue']}
df2 = pd.DataFrame(array2)
df

I want to merge df1 & df2 into df in the real world I have hundreds of df1, df2, df3...), so the output would be:

    name    day              number color
0   Trevor  2020-10-11 08:00:00 5   red
1   John    2020-10-11 08:00:00 0   blue
2   Trevor  2020-10-11 08:00:00 3   red
3   John    2020-10-11 08:00:00 5   blue
4   Trevor  2020-10-11 08:00:00 1   red
5   Trevor  2020-10-11 12:34:00 8   red
6   John    2020-10-11 09:12:00 6   blue
2
  • Have you looked through this post? stackoverflow.com/questions/19125091/… Commented Feb 17, 2021 at 21:29
  • Yes. This is not what I am looking for Commented Feb 17, 2021 at 21:30

2 Answers 2

1

Looks like merge and concat:

df.merge(pd.concat([df1,df2]), on='name', how='left')

Output:

     name                  day  number color
0  Trevor  2020-10-11 08:00:00       5   red
1    John  2020-10-11 08:00:00       0  blue
2  Trevor  2020-10-11 08:00:00       3   red
3    John  2020-10-11 08:00:00       5  blue
4  Trevor  2020-10-11 08:00:00       1   red
5  Trevor  2020-10-11 12:34:00       8   red
6    John  2020-10-11 09:12:00       6  blue

Update: Try loc update every time you get a new df#:

df.loc[df['name'].isin(df1['name']), 'color'] = df1['color'].iloc[0]

df.loc[df['name'].isin(df2['name']), 'color'] = df2['color'].iloc[0]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, BUT I have hundreds of df# - not just two
How do you store that hundreds of df#? you can easily pass the list pd.concat(list_of_dfs_)?
Problem is I get them one at a time and need to "merge" them one at a time to df
@gtomer you should mention that in your question. See updated answer if it helps.
current_df = current_df.merge(pd.concat([current_df, new_df]), ....
|
1
pd.merge(df,df1.append(df2), how='left', on='name')

2 Comments

Thanks, BUT I have hundreds of df# - not just two
Parse them as a list. Concat become more useful then

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.