4

I'm wondering how to get the Date column values. The 'Date' column doesn't show when typing df.columns. I'm trying to get this df into a Json file with df.to_json(), which get all values but the Date. Thank you all.

             Open   High    Low  Close
Date
2020-03-16  36.01  41.40  36.01  39.04
2020-03-17  40.50  43.57  39.11  41.51
2020-03-18  38.80  40.87  35.70  38.65
2020-03-19  37.50  39.00  35.26  36.40
2020-03-20  37.90  39.41  34.71  35.19

In [49]: type(df) Out[49]: pandas.core.frame.DataFrame

Sam
  • 86
  • 1
  • 5

2 Answers2

11

Looks like Date is currently in the index. If you would like it to be a column, you just have to reset the index:

df.reset_index(inplace=True)
kgoettler
  • 169
  • 4
  • 2
    Thank you! I didn't know I could have values on the index. You were really helpful! :) – Sam Apr 16 '20 at 17:33
1

It doesn't appear because it's not really a column, it's the index. It shouldn't matter, however, because by default, df.to_json() will by default include the index. It says so in the docs.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Thank you! Some how it's not including. But if I reset_index like @kgoettler said above it worked! :) – Sam Apr 16 '20 at 17:34