Created
June 4, 2021 22:16
-
-
Save roganjoshp/ed69ce537ab4ee3b914cbe52a44a7a06 to your computer and use it in GitHub Desktop.
Pandas padding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import datetime as dt | |
import pandas as pd | |
df = pd.DataFrame({'date': ['2021-01-02', '2021-01-03', '2021-01-04', | |
'2021-01-01', '2021-01-03', '2021-01-04', | |
'2021-01-01', '2021-01-02', '2021-01-04', | |
'2021-01-01', '2021-01-02', '2021-01-03'], | |
'plant': [1, 1, 1, | |
2, 2, 2, | |
1, 1, 1, | |
2, 2, 2], | |
'product': [4, 4, 4, | |
5, 5, 5, | |
5, 5, 5, | |
4, 4, 4], | |
'value': [random.randint(1, 100) for x in range(12)]}) | |
exaggerated_start_date = dt.date(2020, 12, 30).strftime('%Y-%m-%d') | |
exaggerated_end_date = dt.date(2021, 1, 8).strftime('%Y-%m-%d') | |
extra = pd.DataFrame({'date': [exaggerated_start_date, exaggerated_end_date], | |
'plant': [-1, -1], | |
'product': [-1, -1], | |
'value': [-1, -1]}) | |
df = df.append(extra) | |
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d') | |
df.set_index(df['date'], inplace=True) | |
df = (df.groupby(['plant', 'product']) | |
.resample('D')['value'] | |
.first() | |
.fillna(0) | |
.astype(int) | |
.reset_index()) | |
df = (df.pivot(index='date', columns=['plant', 'product'], values='value') | |
.unstack() | |
.fillna(0) | |
.reset_index()) | |
df = df.rename(columns={0: 'value'}) | |
df = df[(df['plant'] != -1) & (df['product'] != -1)] | |
print(df.head(50)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment