简体   繁体   中英

How to update columns values based on another column

If I have two dataframe such as follows, and want to update the values of first dataframe based on matching date from second dataframe, and also include rows, which are not in first dataframe. Could anyone help me with the solution.

first_df =
|  date | value
| 01/01 | 10
| 01/02 | 20
| 01/03 | 30
| 01/04 | 40
| 01/05 | 50


second_df = 
|  date | value
| 01/02 | 1
| 01/03 | 2
| 01/04 | 3
| 01/05 | 4
| 01/06 | 5

expected = 
|  date | value
| 01/01 | 10
| 01/02 | 1
| 01/03 | 2
| 01/04 | 3
| 01/05 | 4
| 01/06 | 5

It seems that you keep the value in second_df and add new row(date, value) from first_df .

I will try to merge two dataframes, and fill NaN value in one column with another column. You can also remove redundant column, and rename columns.

df = pd.merge(left=second_df, right=first_df, how='outer', on='date')
df.value_x.fillna(df.value_y, inplace=True)
del df['value_y']
df.columns = ['date', 'value']

try this:

result = first_df.reindex(first_df.index.append(second_df.index).unique())
result.loc[second_df.index] = second_df
>>>
        value
date    
01/01   10.0
01/02   1.0
01/03   2.0
01/04   3.0
01/05   4.0
01/06   5.0

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