Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas DataFrame column not showing on df.columns [duplicate]

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

like image 678
Sam Avatar asked Sep 05 '25 03:09

Sam


2 Answers

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)
like image 170
kgoettler Avatar answered Sep 07 '25 20:09

kgoettler


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.

like image 26
Nicolas Gervais Avatar answered Sep 07 '25 19:09

Nicolas Gervais