1

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!

2
  • 1
    please try eth.loc[eth.columnname=='2020-08-13', 'Value']. where columnname is maybe date. Alternatively post the eth sample dataframe Commented Aug 16, 2020 at 1:06
  • 1
    According to the docs, df.loc will raise a Key Error if any items are not found. It seems you don't have '2020-08-13', if you share a snippet of the data we could help you better. Commented Aug 16, 2020 at 1:08

2 Answers 2

2

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:

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

You can get items with date say 2020-08-13:

eth.loc[eth['Date'] == '2020-08-13']
Sign up to request clarification or add additional context in comments.

Comments

1

Answer turned out to be eth.loc[eth.columnname=='2020-08-13', 'Value'], thanks to @wwnde for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.