简体   繁体   English

在Python中按周数绘制散点图

[英]plotting scatter plot by weeks of days in python

I have simpe dataset in a dataframe in which year, attendance, weeks are the column. 我在一个数据帧中有simpe数据集,其中的年,出勤率,周为列。

attendance    week      years
37440 Sun 2010-04-04 43504 Mon 2010-04-05 38935 Mon 2010-04-05 40052 Mon 2010-04-05 43510 Tue 2010-04-06 38000 Tue 2010-04-06 10090 Tue 2010-04-06 41533 Wed 2010-04-07

i would like to plot a scatter,i have many values of attancdance against each day i would like to average them and show them on scatter plot. 我想绘制一个散点图,我每天有很多attancdance值,我想将它们取平均值并在散点图上显示它们。
i saw this on other post and i tried but it gave an error here is my code 我在其他帖子上看到了这个并且尝试了但是它给出了错误,这是我的代码

import pandas as pd

days=['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
log_2010=pd.read_excel('GL2010-2017.xlsx')

year=log_2010['years']
attendance=log_2010['attendace']
week=log_2010['day_of_week']
df=pd.DataFrame({
    'years':year,
       'attendance':attendance,
       'week':week
    })
new_df=df.dropna(how='any')
new_df['years']=pd.to_datetime(year,format='%Y%m%d')
df['week'] = pd.Categorical(new_df['week'], categories=days)


df[['week', 'attendance']].groupby('week').mean().plot.scatter(df['week'],df['attendance'])

i get this error 我得到这个错误
KeyError: "['Sun' 'Mon' 'Mon' ... 'Sun' 'Sun' 'Sun'] not in index" KeyError:“ ['Sun''Mon''Mon'...'Sun''Sun''Sun']不在索引中”

从Excel文件导入数据时,尝试将星期设置为索引:log_2010 = pd.read_excel('GL2010-2017.xlsx',index_col ='week')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM