1

I have a dataframe like this:

Id    date          sales  sales_new
291   2022-03-01     10       15 
292   2022-04-01     12       16
293   2022-05-01      9        0 
294   2022-06-01     13       20
295   2022-07-01     10       nan
296   2022-08-01     12       nan

I would like replace the values if they exist

Outcome desire:

Id    date          sales  
291   2022-03-01     15       
292   2022-04-01     16      
293   2022-05-01      0       
294   2022-06-01     20      
295   2022-07-01     10       
296   2022-08-01     12

3 Answers3

1

You can use update to do that:

df['sales'].update(df['sales_new'])
df.drop(columns='sales_new')

Result

    Id        date  sales
0  291  2022-03-01     15
1  292  2022-04-01     16
2  293  2022-05-01      0
3  294  2022-06-01     20
4  295  2022-07-01     10
5  296  2022-08-01     12
jch
  • 3,600
  • 1
  • 15
  • 17
0

You can use this:

import numpy as np
import pandas as pd


data = [[291, "2022-03-01", 10, 15],
        [292, "2022-04-01", 12, 16],
        [293, "2022-05-01", 9,  0],
        [294, "2022-06-01", 13, 20],
        [295, "2022-07-01", 10, np.nan],
        [296, "2022-08-01", 12, np.nan]]

df = pd.DataFrame(data, columns=["Id", "date", "sales", "sales_new"])

df["sales"] = df.apply(lambda row: row["sales_new"] if not pd.isna(row["sales_new"]) else row["sales"], axis=1)
bitflip
  • 3,436
  • 1
  • 3
  • 22
0
df["sales"] = df["sales_new"].fillna(df["sales"])
del df["sales_new"]
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • The answer from @jch is better. – Stuart Berg Sep 21 '22 at 23:55
  • 1
    You can [pop](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pop.html) to remove the column at the same time: `df["sales"] = df.pop("sales_new").fillna(df["sales"], downcast='infer')` – Henry Ecker Sep 22 '22 at 02:20