I have 3 dataframes (df1, df2 & df3), the main one (df1) and two additional ones which contain 1 column amongst others that I want to bring over to the main dataframe.
Sample dfs:
JavaScript
x
12
12
1
df1 = {'Col_f': ['Georgia', 'Nevada', 'New York', 'Texas', 'Arizona'],
2
'Col_g': ['SUV', 'Coupe', 'Wagon', 'Crossover', 'Sedan']}
3
df1 = pd.DataFrame(df1)
4
5
df2 = {'Col_g': ['SUV', '4x4', 'Wagon', 'Truck', 'Sedan'],
6
'Objective': ['15%', '13%', '55%', '40.4%', '2.48%']}
7
df2 = pd.DataFrame(df2)
8
9
df3 = {'Col_f': ['Georgia', 'California', 'Pennsylvania', 'Texas', 'Arizona'],
10
'Objective': ['15%', '13%', '55%', '40.4%', '2.48%']}
11
df3 = pd.DataFrame(df3)
12
I am using the following code:
JavaScript
1
8
1
df1_new = pd.merge(df1, df2, on = 'Col_g', how = 'left')
2
3
Which returns the following df:
4
5
df_new = {'Col_f': ['Georgia', 'California', 'Pennsylvania', 'Texas', 'Arizona'],
6
'Col_g': ['SUV', 'Coupe', 'Wagon', 'Crossover', 'Sedan'],
7
'Objective': ['15%', ' ', '55%', ' ', '2.48%']}
8
Then for the two empty strings for the “Objective” column I want to continue with a second merge (or an excel vlookup which I guess are the same), to fill in those empty ones.
Code that I thought would be used for the second merge:
JavaScript
1
2
1
df1_newer = pd.merge(df_new, df3, on = 'Col_f', how = 'left')
2
Desired final output.
JavaScript
1
4
1
df_newer = {'Col_f': ['Georgia', 'California', 'Pennsylvania', 'Texas', 'Arizona'],
2
'Col_g': ['SUV', '4x4', 'Wagon', 'Truck', 'Sedan'],
3
'Objective': ['15%', '13%', '55%', '40.4%', '2.48%']}
4
Any suggestions would be more than appreciated!
Advertisement
Answer
After merging the three dataframes, you can use mask
or np.where
to conditionally assign value
JavaScript
1
3
1
df1_new = pd.merge(df1, df2, on='Col_g', how='left')['Objective']
2
df1_newer = pd.merge(df1, df3, on='Col_f', how='left')['Objective']
3
JavaScript
1
18
18
1
print(df1_new)
2
3
0 NaN
4
1 NaN
5
2 55%
6
3 NaN
7
4 2.48%
8
Name: Objective, dtype: object
9
10
print(df1_newer)
11
12
0 15%
13
1 NaN
14
2 NaN
15
3 40.4%
16
4 2.48%
17
Name: Objective, dtype: object
18
JavaScript
1
8
1
df1['Objective'] = df1_new.mask(df1_new.isna(), df1_newer)
2
3
#or
4
5
import numpy as np
6
7
df1['Objective'] = np.where(df1_new.isna(), df1_newer, df1_new)
8
JavaScript
1
9
1
print(df1)
2
3
Col_f Col_g Objective
4
0 Georgia Minivan 15%
5
1 Nevada Coupe NaN
6
2 New York Wagon 55%
7
3 Texas Crossover 40.4%
8
4 Arizona Sedan 2.48%
9