Skip to content

Instantly share code, notes, and snippets.

@rpanai
Last active July 24, 2018 23:53
Show Gist options
  • Select an option

  • Save rpanai/ea9828f92b0ba6a4c6e8f5df2f081386 to your computer and use it in GitHub Desktop.

Select an option

Save rpanai/ea9828f92b0ba6a4c6e8f5df2f081386 to your computer and use it in GitHub Desktop.
Fast Flexible Easy Pandas
import pandas as pd
import numpy as np
# define diz
diz_prices = {0:12, 1:20, 2:28}
out = pd.DataFrame(index=np.arange(25))
out["price"] = np.digitize(out.index.values , bins=[7,17,24])
out["price"] = out["price"].map(diz_prices)
diz = out.to_dict()["price"]
def apply_tariff_vectorize(df, diz):
df['cost_cents'] = df["date_time"].dt.hour.map(diz) * df["energy_kwh"].values
df = pd.read_csv("https://raw.githubusercontent.com/realpython/materials/master/pandas-fast-flexible-intuitive/tutorial/demand_profile.csv",
parse_dates=["date_time"])
apply_tariff_vectorize(df, diz)
@kurniantoska
Copy link

What is that mean np.arange(25) , why we need array 0 to 25??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment