[英]plotting scatter plot by weeks of days in python
我在一個數據幀中有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
我想繪制一個散點圖,我每天有很多attancdance值,我想將它們取平均值並在散點圖上顯示它們。
我在其他帖子上看到了這個並且嘗試了但是它給出了錯誤,這是我的代碼
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'])
我得到這個錯誤
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.