Skip to content

Instantly share code, notes, and snippets.

@notionparallax
Created June 27, 2021 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save notionparallax/4c46a8f31b19cdfc54870e4ed46e99f7 to your computer and use it in GitHub Desktop.
Save notionparallax/4c46a8f31b19cdfc54870e4ed46e99f7 to your computer and use it in GitHub Desktop.
#%%
from numpy import NaN # NaN means not a number
import pandas as pd
#%%
price_df = pd.read_excel(
"price_history_checks_may2021.xlsx", parse_dates=["PriceUpdatedDate"]
)
price_df.head()
#%%
fuel_codes = price_df.FuelCode.unique()
fuel_codes = [x for x in fuel_codes if type(x) is str]
print(fuel_codes)
#%%
new_column_names = (
[f"Price_{name}_UpdatedDate" for name in fuel_codes]
+ [f"Price_{name}" for name in fuel_codes]
+ [
name
for name in price_df.columns
if name not in ["FuelCode", "PriceUpdatedDate", "Price"]
]
)
new_column_names.sort()
print(new_column_names)
#%%
#%%
new_rows = []
temp_row = pd.Series(index=new_column_names, dtype=object)
for i, row in price_df.head(100).iterrows():
fc = row.FuelCode
if row.ServiceStationName is not NaN:
new_rows.append(temp_row)
temp_row = pd.Series(index=new_column_names, dtype=object)
for key, element in row.iteritems():
if key in temp_row.index:
temp_row[key] = element
temp_row[f"Price_{fc}_UpdatedDate"] = row.PriceUpdatedDate
temp_row[f"Price_{fc}"] = row.Price
elif row.ServiceStationName is NaN:
temp_row[f"Price_{fc}_UpdatedDate"] = row.PriceUpdatedDate
temp_row[f"Price_{fc}"] = row.Price
#%%
reshaped_df = pd.DataFrame(new_rows)
reshaped_df.drop([0], inplace=True)
reshaped_df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment