Skip to content

Instantly share code, notes, and snippets.

@Seanny123
Created July 6, 2023 14:33
Show Gist options
  • Save Seanny123/f7953c174badf69fbe97188e5b69eec2 to your computer and use it in GitHub Desktop.
Save Seanny123/f7953c174badf69fbe97188e5b69eec2 to your computer and use it in GitHub Desktop.
Pandas example using `explode()`
Date Owners Spent
05/23/2022 Melissa;Jenn 10
05/23/2022 Jenn 30
05/24/2022 Melissa 120
05/24/2022 Alexander 20
06/23/2022 Alexander;Jenn 10
06/23/2022 Jenn 30
06/24/2022 Jenn 120
import pandas as pd
df = pd.read_csv('log.csv')
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')
df['Owners'] = df['Owners'].str.split(';')
df = df.explode('Owners')
df['Month'] = df['Date'].dt.month
print(df)
avg_time_spent = df.groupby(['Month', 'Owners'])['Spent'].mean().reset_index()
print(avg_time_spent)
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
plt.figure(figsize=(10, 6))
sns.lineplot(data=avg_time_spent, x='Month', y='Spent', hue='Owners', style='Owners', marker='o')
plt.title('Average Time Spent per Month')
plt.xlabel('Month')
plt.ylabel('Average Time Spent')
plt.legend(title='Owners')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment