2016-11-29 38 views
2

天均值以下代碼如何找到基於

test_df['Started'] = pd.to_datetime(test_df['Started']) 
test_df['day count'] = test_df['Started'].apply(lambda x: x.strftime('%A')) 
test_day_count = test_df['day count'].value_counts() 
print(test_day_count) 

返回

Thursday  25 
Friday  19 
Saturday  13 

這些值開始對天的測試次數。我想查找一週中每天的平均()測試分數。

我試圖與其中的成績位於[]

test_df['Started'] = pd.to_datetime(test_df['Started']) 
test_df['day count'] = test_df['Started'].apply(lambda x:x.strftime('%A')) 
test_day_count = test_df['day count'].value_counts().mean(test_df['marks']) 
print(test_day_count) 

我得到的錯誤 類型錯誤的科拉姆的名稱一起添加均值與第三行:「系列」的對象是可變的,因此,他們不能被散列

回答

1

使用strftime('%A')groupby參數:

icma_df.marks.groupby(icma_df['Started'].dt.strftime('%A')).mean() 

示範

icma_df = pd.DataFrame(dict(marks=np.random.rand(100), 
          Started=pd.date_range('2012-12-31', periods=100, freq='B'))) 

icma_df.marks.groupby(icma_df['Started'].dt.strftime('%A')).mean() 

正如指出的@root,這也適用,看起來更好,可能會比較快

icma_df.marks.groupby(icma_df['Started'].dt.weekday_name).mean() 

Started 
Friday  0.428581 
Monday  0.443394 
Thursday  0.485658 
Tuesday  0.325027 
Wednesday 0.506592 
Name: marks, dtype: float64 
+1

你也可以使用'.dt.weekday_name',而不是'.DT。的strftime( '%A')'。 – root