【发布时间】:2020-05-08 16:35:34
【问题描述】:
我通过过滤器获得所需的日期“2013-09-12”行。是不是因为标签“日期”是一个不合时宜的标签,而不合时宜是“2013-09-12”日期。我相信,'.loc' 正在查看列和行标签值。我的理解正确吗?
mean_altitudes_perday = grouped_bydates.altitude.mean()
print(mean_altitudes_perday)
print(mean_altitudes_perday.loc[mean_altitudes_perday['date']] == '2013-09-12') # Why loc is not working here?
# Q: What is the mean altitude of the birds on 2013-09-12?
# A: print(mean_altitudes_perday.filter(like='2013-09-12', axis=0))
错误信息:
date
2013-08-15 134.092000
2013-08-16 134.839506
2013-08-17 147.439024
2013-08-18 129.608163
2013-08-19 180.174797
...
2014-04-26 15.118012
2014-04-27 23.897297
2014-04-28 37.716867
2014-04-29 19.244792
2014-04-30 13.954545
Name: altitude, Length: 259, dtype: float64
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4410 try:
-> 4411 return libindex.get_value_at(s, key)
4412 except IndexError:
# Had to delete a lot of error code due to posting requirements
KeyError: 'date'
【问题讨论】:
-
在您的系列中,
date是一个索引,而不是一个列。可以看Series.loc的语法,需要mean_altitudes_perday.loc['2013-09-12']