0

I have a dataframe (df_final) with 2 indexes (Mineral and Year).

                         Developed Countries   ...  Transition Countries 
Mineral      Year                              ...                       
Iron Fe      2014-01-01             543730900  ...              119680450
             2015-01-01             580394662  ...              113540570
             2016-01-01             608537133  ...              108977300
             2017-01-01             632252158  ...              109270430
             2018-01-01             648117225  ...              110999150
Chromium CrO 2014-01-01                451819  ...                2669000
             2015-01-01                457100  ...                2502900
             2016-01-01                469140  ...                2493000
             2017-01-01                416285  ...                2740200
             2018-01-01                509424  ...                3078100
Cobalt       2014-01-01                 15383  ...                   5400
             2015-01-01                 16298  ...                   5800
             2016-01-01                 15281  ...                   5600
             2017-01-01                 14032  ...                   4700
             2018-01-01                 12353  ...                   5330

Data gets returned perfectly fine with this query:

df_final.loc['Gold','2014']

However, I get a

KeyError: '2014'

with this query:

df_final.loc[:,'2014']

Any ideas?

6
  • 2
    Can you add some data sample? Commented Aug 24, 2020 at 9:20
  • You can just replace 'Gold' with 'Cobalt' in the referenced query and added dataset... Commented Aug 24, 2020 at 9:33
  • are you trying to get all columns of all Mineral with Year = '2014' data? Commented Aug 24, 2020 at 9:39
  • yes - all minerals must be returned for the year 2014 Commented Aug 24, 2020 at 9:59
  • @pinegulf - s.loc[ :,'2014' , ] this still gives me the same error Commented Aug 24, 2020 at 10:07

1 Answer 1

1

you can use this to get all Mineral with Year = '2014' :

df_final.loc[(df_final.index.levels[0], '2014'), :]
Sign up to request clarification or add additional context in comments.

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.