简体   繁体   English

pandas.DataFrame.loc 不适用于索引日期

[英]pandas.DataFrame.loc won't work for indexing dates

I'm trying to look for a piece of data by date and value, however, I keep getting an error.我正在尝试按日期和值查找一条数据,但是,我不断收到错误消息。 Here is the code:这是代码:

eth.loc['2020-08-13', 'Value']

Here is the error:这是错误:


        352                 except ValueError as err:
        353                     raise KeyError(key) from err
    --> 354             raise KeyError(key)
        355         return super().get_loc(key, method=method, tolerance=tolerance)
        356 
    
    KeyError: '2020-08-13'

Thanks!谢谢!

Assumming you've a dataset defined by:假设您有一个由以下内容定义的数据集:

eth = pd.DataFrame([['2020-08-13', 2], ['2020-08-14', 5], ['2020-08-15', 8]], 
                    index=['book1','book2','book3'], 
                    columns=['Date','Value'])

You can get items with value say 2:您可以获得值为 2 的项目:

eth.loc[eth['Value'] == 2]

You can get items with date say 2020-08-13:您可以获得日期为 2020-08-13 的项目:

eth.loc[eth['Date'] == '2020-08-13']

Answer turned out to be eth.loc[eth.columnname=='2020-08-13', 'Value'] , thanks to @wwnde for the help!答案原来是eth.loc[eth.columnname=='2020-08-13', 'Value'] ,感谢@wwnde 的帮助!

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

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