I have two pandas dataframes that look like this:
df1
id 10 20 30
0 2020-04-01-10001 0.0 0.000000 0.000000
1 2020-04-01-10003 0.0 0.026587 0.053174
2 2020-04-01-10005 0.0 0.030884 0.061768
3 2020-04-01-1001 0.0 0.035875 0.071751
4 2020-04-01-1003 0.0 0.041673 0.083346
...
df2
10 20 30
id
2020-04-01-10001 0.15 0.17 0.18
2020-04-01-10003 0.55 0.57 0.61
2020-04-01-10005 0.36 0.37 0.38
2020-04-01-1001 0.00 0.00 0.02
2020-04-01-1003 0.00 0.02 0.04
...
I want to use rows of df1 to update the rows of df2, so I am trying to use df2.update(df1). Unfortunately, this doesn't change the rows of df2. I suspect that it has something to do with the extra numbers on the left side of df1. I'm not sure what those are. I also noticed that the two tables give different results when I run df.to_dict:
'10': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},,...
{'10': {'2020-04-01-10001': 0.15,
'2020-04-01-10003': 0.55,
'2020-04-01-10005': 0.36,
'2020-04-01-1001': 0.0,
'2020-04-01-1003': 0.0},...
How would I go about converting df1 to the format of df2?
Use set_index
with replace
and fillna
:
import numpy as np
df1 = df1.set_index('id').replace(0, np.NaN).fillna(df2)
10 20 30
id
2020-04-01-10001 0.15 0.170000 0.180000
2020-04-01-10003 0.55 0.026587 0.053174
2020-04-01-10005 0.36 0.030884 0.061768
2020-04-01-1001 0.00 0.035875 0.071751
2020-04-01-1003 0.00 0.041673 0.083346
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.