I am trying to create a dataframe which has an addition of dates (which is and index) from an orginial dataframe which looks like this:
df_galletas.head(10)
| | Sales || Monday | .... || Saturday | Januray || ... | Promo |
| -------- | -------- || -------- | -------- || -------- | -------- || -------- | -------- |
| 2021-05-02 | 5 || 0 | ... || 0 | 0 || ... | 1 |
| 2021-05-03 | 6 || 1 | ... || 0 | 0 || ... | 0 |
The dataframe should look like this, with a range of 15 days from the last day of this dataframe.
Can anyone help me?
CodePudding user response:
Is this what you're looking for?
import pandas as pd
import numpy as np
nrows = 15
df = pd.DataFrame(
{
'Sales': np.random.randint(1, 10, nrows),
'Monday': [0] * nrows,
'Tuesday': [0] * nrows,
'Wednesday': [0] * nrows,
'Thursday': [0] * nrows,
'Friday': [0] * nrows,
'Saturday': [0] * nrows,
'Sunday': [0] * nrows,
'January': [0] * nrows,
'February': [0] * nrows,
'March': [0] * nrows,
'April': [0] * nrows,
'May': [0] * nrows,
'June': [0] * nrows,
'July': [0] * nrows,
'August': [0] * nrows,
'September': [0] * nrows,
'October': [0] * nrows,
'November': [0] * nrows,
'December': [0] * nrows,
'Promo': np.random.randint(0, 2, nrows),
},
index = pd.date_range('2021-05-02', periods=nrows, freq='D')
)
for index, value in enumerate(df.index):
df.iloc[index][pd.to_datetime(value).day_name()] = 1
df.iloc[index][pd.to_datetime(value).month_name()] = 1
df
Outputs: