Created using the following ChatGPT conversation: https://chat.openai.com/share/9cac1fad-851d-4f43-87da-17d64b3f4e02
Created
July 6, 2023 14:33
-
-
Save Seanny123/f7953c174badf69fbe97188e5b69eec2 to your computer and use it in GitHub Desktop.
Pandas example using `explode()`
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
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 |
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 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