Home > Software design >  How to update columns values based on another column
How to update columns values based on another column

Time:01-08

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

CodePudding user response:

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']

CodePudding user response:

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
  •  Tags:  
  • Related