How to get the mean of value_counts in dataframe column

Viewed 34

I have a dataframe that looks like this:
DF

I want to get the average rides per day_of_week.
I tried to make this: df.groupby('member_casual')['day_of_week'].value_counts().mean()
the output was just a number. How can I get the average rides for each weekday?

1 Answers

This is something I was able to put together really quick, you could probably make it a little cleaner if you wanted to put more effort into it. We could also help you a little more if you provided a little extra data

df = pd.DataFrame({
    'Date' : ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08']
})

df['Date'] = pd.to_datetime(df['Date'])
#df['Day_Name'] = df['Date'].dt.day_name() #If you wanted to verify it was only monday-friday
df['Day_Number'] = df['Date'].dt.weekday
df['Week_Number'] = df['Date'].dt.isocalendar().week
df = df.loc[np.where(df['Day_Number'].between(0, 5), True, False)].copy()
df = df.groupby('Week_Number').count().reset_index()[['Week_Number', 'Date']]
df['Avg_Week'] = df['Date'].agg('mean')
df
Related