0

I am attempting to index a pandas dataframe by a column in the dataframe and get KeyError. For example, the dataframe has rows indexed on general ledger account name ('Cash','Securities', etc) and the columns are indexed by the time period ('Q1_2018', 'Q2_2018', etc.). These values are confirmed when I use df.index and df.columns. However, when i run df.loc['Cash','Q1_2018'], it returns the following KeyError: 'Q1_2018'. Any thoughts on why this would occur.

            Q1_2018     Q2_2018     Q3_2018
Cash        2000000     2000100     1900000
Securites   3000000     2999900     2910000
carrie3zz
  • 43
  • 9

1 Answers1

0

I see that you have used iloc with labels of your dataframe. Do note that since you are using labels, you should be trying with loc instead.

Here are some pages that might interest you on how both methods work:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html https://pandas.pydata.org/pandas-docs/version/0.25/reference/api/pandas.DataFrame.iloc.html

loc vs iloc How are iloc, ix and loc different?

cyneo
  • 876
  • 7
  • 9