Let's say I have a dataframe with columns: "Date, Price".
I wish to achieve this:
If the value of the date in the "Date" column of the dataframe falls between April and October, I want to create a new column:
df['Price_new'] = df[Price] 0.01
And if it falls between November and March:
df['Price_new'] = df['Price'] 0.12
CodePudding user response:
First, you need to have your dates in datetime format. If they're not already datetimes, you can convert them with pd.to_datetime.
df = pd.DataFrame({'Date':['17-2-21', '1-4-21', '21-6-21', '15-9-21', '17-2-22'], 'Price': [10.2, 12.5, 7.0, 3.8, 8.8]})
df.Date = pd.to_datetime(df.Date)
df
| index | Date | Price |
|---|---|---|
| 0 | 2021-02-17 | 10.2 |
| 1 | 2021-01-04 | 12.5 |
| 2 | 2021-06-21 | 7.0 |
| 3 | 2021-09-15 | 3.8 |
| 4 | 2022-02-17 | 8.8 |
Then you can use apply in a similar way than here:
df.loc[(df['Date'].dt.month >= 4) & (df['Date'].dt.month <= 10),'Price_new'] = df.loc[(df['Date'].dt.month >= 4) & (df['Date'].dt.month <= 10),'Price'].apply(lambda x: x 0.01)
df.loc[(df['Date'].dt.month <= 3) | (df['Date'].dt.month >= 11),'Price_new'] = df.loc[(df['Date'].dt.month <= 3) | (df['Date'].dt.month >= 11),'Price'].apply(lambda x: x 0.12)
df
| index | Date | Price | Price_new |
|---|---|---|---|
| 0 | 2021-02-17 | 10.2 | 10.32 |
| 1 | 2021-01-04 | 12.5 | 12.62 |
| 2 | 2021-06-21 | 7.0 | 7.01 |
| 3 | 2021-09-15 | 3.8 | 3.81 |
| 4 | 2022-02-17 | 8.8 | 8.92 |
