'pandas replace nan multiple fields with another dataframe [duplicate]
I want to replace some nan fields in the first dataframe with another dataframe. first dataframe like this:
data={'month':['2021-10','2021-11','2021-12','2022-01','2021-02'],
'sessions':[10000,25000,32684,7415,None],
'users':[2500,3600,4120,2154,None],
'new_users':[750,840,980,874,680],
'cr':[1.2,0.9,0.7,None,0.78}
df1=pd.DataFrame(data)
df1
month sessions users new_users cr
2021-10 10000 2500 750 1.2
2021-11 25000 3600 840 0.9
2021-12 32684 4120 980 0.7
2022-01 7415 2154 974 nan
2022-02 nan nan 680 0.78
df2:
data2={'month':['2021-12','2022-01','2021-02'],
'sessions':[32684,7415,4122],
'users':[4120,3650,4120]
'cr':[0.7,0.85,0.78]}
df2=pd.DataFrame(data2)
df2
month sessions users cr
2021-12 32684 4120 0.7
2022-01 7415 3650 0.85
2022-02 4122 4120 0.78
expected output:
month sessions users new_users cr
2021-10 10000 2500 750 1.2
2021-11 25000 3600 840 0.9
2021-12 32684 4120 980 0.7
2022-01 7415 2154 974 0.85
2022-02 4122 4120 680 0.78
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
