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
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
df["sales"] = df["sales_new"].fillna(df["sales"])
del df["sales_new"]