简体   繁体   中英

Replace selected cell values in a dataframe with values from another separate dataframe

I have a dataframe (df1) that looks like this:

+------------+--------+-------+
| Date       | Length | Width |
+------------+--------+-------+
| 2020-01-01 | 10     | 12    |
+------------+--------+-------+
| 2020-01-02 | 39     | 34    |
+------------+--------+-------+
| 2020-01-03 | 50     | 23    |
+------------+--------+-------+
| 2020-01-04 | 1      | 24    |
+------------+--------+-------+
| 2020-01-05 | 2      | 10    |
+------------+--------+-------+
| 2020-01-06 | 1      | 16    |
+------------+--------+-------+
| 2020-01-07 | 79     | 20    |
+------------+--------+-------+
| 2020-01-08 | 86     | 34    |
+------------+--------+-------+
| 2020-01-09 | 92     | 23    |
+------------+--------+-------+
| 2020-01-10 | 101    | 25    |
+------------+--------+-------+
| 2020-01-11 | 113    | 24    |
+------------+--------+-------+
| 2020-01-12 | 125    | 50    |
+------------+--------+-------+
| ...        | ...    |       |
+------------+--------+-------+

The value for date "2020-01-04" to "2020-01-06" under "Length" column is not what I want.

I found the correct values for those 3 dates and arranged them in a separate small table like this (df2):


+------------+--------+
| Date       | Length |
+------------+--------+
| 2020-01-04 | 20     |
+------------+--------+
| 2020-01-05 | 30     |
+------------+--------+
| 2020-01-06 | 50     |
+------------+--------+

What is the most efficient way for me to replace the 3 values back to df1?

This is just a pseudo dataset I created to illustrate. The real data I have is much larger than this (both df1 and df2 are much larger), so I can't possibly manually replaced those values cell by cell.

I expect the end results to look like this:


+------------+--------+-------+
| Date       | Length | Width |
+------------+--------+-------+
| 2020-01-01 | 10     | 12    |
+------------+--------+-------+
| 2020-01-02 | 39     | 34    |
+------------+--------+-------+
| 2020-01-03 | 50     | 23    |
+------------+--------+-------+
| 2020-01-04 | 20     | 24    |
+------------+--------+-------+
| 2020-01-05 | 30     | 10    |
+------------+--------+-------+
| 2020-01-06 | 50     | 16    |
+------------+--------+-------+
| 2020-01-07 | 79     | 20    |
+------------+--------+-------+
| 2020-01-08 | 86     | 34    |
+------------+--------+-------+
| 2020-01-09 | 92     | 23    |
+------------+--------+-------+
| 2020-01-10 | 101    | 25    |
+------------+--------+-------+
| 2020-01-11 | 113    | 24    |
+------------+--------+-------+
| 2020-01-12 | 125    | 50    |
+------------+--------+-------+
| ...        | ...    |       |
+------------+--------+-------+

Thanks so much for your help!

Have a look at DataFrame.update() :

# note: update() requires the DataFrames to have indices

df1.set_index('Date', inplace=True)
df1.update(df2.set_index('Date'))
df1.reset_index(inplace=True)

If your indexes are indeed aligned, we can use combine_first

#df1 = df1.set_index('Date')
#df2 = df2.set_index('Date')
df3 = df2.combine_first(df1)

print(df3)

              Length    Width
Date                         
2020-01-01   10        12    
2020-01-02   39        34    
2020-01-03   50        23    
2020-01-04   20        24    
2020-01-05   30        10    
2020-01-06   50        16    
2020-01-07   79        20    
2020-01-08   86        34    
2020-01-09   92        23    
2020-01-10   101       25    
2020-01-11   113       24    
2020-01-12   125       50  

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM