0

I am making my DataFrame like this:

influenza_data = pd.DataFrame(data, columns = ['year', 'week', 'weekly_infections'])

and then I create MultiIndex from year and week columns:

influenza_data = influenza_data.set_index(['year', 'week'])

If I have MultiIndex my DataFrame looks like this:

          weekly_infections
year week                  
2009 40                6600
     41                7100
     42                7700
     43                8300
     44                8600
...                     ...
2019 10                8900
     11                6200
     12                5500
     13                3900
     14                3300

and data_influenza.columns:

Index(['weekly_infections'], dtype='object')

The problem I have is that I can't access year and week columns now.

If I try data_influenza['week'] or year I get KeyError: 'week'. I can only do data_influenza.weekly_infections and that returns a whole DataFrame

I know if I remove multiIndex I can easily access them but why can't I data_influenza.year or week with MultiIndex? I specified columns when I was creating Dataframe

4
  • you can access the multiindex with df.loc[2009,:] or df.query("year==2009") or df.query("year==2009 and week==40") ... there are a number of options. You can have a look here and here Commented Dec 2, 2020 at 4:57
  • @sammywemmy what would be equivalent of df['year'] to get whole year column? Commented Dec 2, 2020 at 5:01
  • not sure I understand. The year is already an index, so you should have all the years. Commented Dec 2, 2020 at 5:11
  • year was not an index, that's the thing that was confusing me. I posted my data_influenza.columns and it only shows Index(['weekly_infections'], dtype='object') so the only index I could work with was weakly_infections Commented Dec 2, 2020 at 14:54

1 Answer 1

1

As Pandas documentation says here, you can access MultiIndex object levels by get_level_values(index) method:

influenza_data.index.get_level_values(0)    # year
influenza_data.index.get_level_values(1)    # week

Obviously, the index parameter represents the order of indices.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.