Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active May 4, 2022 19:47
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 michelkana/9ca66d7476d798b7379983cf5eb87043 to your computer and use it in GitHub Desktop.
Save michelkana/9ca66d7476d798b7379983cf5eb87043 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
# load the data
df_train = pd.read_csv('calendar_train.csv')
df_test = pd.read_csv('calendar_test.csv')
# convert dates
def convert_date(df):
df = df[~ df.price.isnull()]
df.date = pd.to_datetime(df.date, format='%m/%d/%y')
df['month'] = df.date.dt.month
df['month_name'] = df.date.dt.month_name()
df['weekday'] = df.date.dt.weekday
df['weekday_name'] = df.date.dt.day_name()
return df
df_train_2 = convert_date(df_train)
# display few listings
df_train_2.head()
# plot average price per month
ax = df_train_2.groupby(['month','month_name']) \
.agg({'price': np.mean}).reset_index() \
.sort_values(by=['month']).plot.bar(x='month_name',
y='price',
color='skyblue',
title='Average price per month')
ax.set_xlabel('month')
ax.set_ylabel('average price')
ax.legend().set_visible(False);
# plot average price per weekday
ax = df_train_2.groupby(['weekday','weekday_name']) \
.agg({'price': np.mean}).reset_index() \
.sort_values(by=['weekday']).plot.bar(x='weekday_name',
y='price',
color='lightgreen',
title='Average price per weekday')
ax.set_xlabel('weekday')
ax.set_ylabel('average price')
ax.legend().set_visible(False);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment