Home > other >  Matplotlib and Pandas Plotting Score average for every year
Matplotlib and Pandas Plotting Score average for every year

Time:05-09

I have pandas Dataframe that looks like this:

Dataframe

I want to scatter plot the average of each 'Score' for how each 'Publish Date' Value, which is [1...10]

I tried to use groupby, mean, etc. But for some reason, this is not working properly.

df.groupby(ds['Publish Date'].dt)["Score"].mean().plot(kind="line",rot=25)

I wish to have a scatter plot, with X as [1...10] and Y as the Score average. I need to take every unique value from ds['Publish Date'], which is values between 1 to 10, and for each one of the, to get the average score. For example - for 10: 2.4, for 9: 4.7 and to have those stats in a scatter plot. Thank you!

Need some help, Thank you!

CodePudding user response:

You could use seaborn to plot it, it's very intuitive.

import pandas as pd
import seaborn as sns
import random

df = pd.DataFrame()
df["Publish_date"] = [random.randrange(0,11) for _ in range(20)]
df["Score"] = [random.randrange(0,60)/10 for _ in range(20)]

df_grouped_by_year = df.groupby(df['Publish_date']).mean()
sns.scatterplot(data = df_grouped_by_year)

enter image description here

  • Related