Chirayu05
Chirayu05

Reputation: 129

Not using Index column as a variable in Pandas DataFrame

I am trying to work on stock data downloaded from NSE website using the below code:

df1 = get_history(symbol= 'RELIANCE' ,
                    start=date(2019,1,1),
                    end=date(2019,1,30))

df = pd.DataFrame(df1)

The dataframe df looks as below:

              Symbol Series  Prev Close     Open     High      Low     Last  \
Date                                                                          
2019-01-01  RELIANCE     EQ     1121.25  1125.25  1127.30  1110.10  1120.50   
2019-01-02  RELIANCE     EQ     1121.00  1114.50  1127.00  1101.00  1104.35   
2019-01-03  RELIANCE     EQ     1106.40  1107.50  1114.60  1090.10  1090.30   
2019-01-04  RELIANCE     EQ     1092.75  1097.40  1104.45  1081.10  1099.60   
2019-01-07  RELIANCE     EQ     1098.65  1107.00  1118.45  1101.00  1104.90   
2019-01-08  RELIANCE     EQ     1104.75  1105.10  1109.95  1096.00  1101.00   
2019-01-09  RELIANCE     EQ     1104.65  1112.00  1117.00  1098.70  1109.25   
2019-01-10  RELIANCE     EQ     1110.75  1107.75  1111.00  1103.00  1107.60   
2019-01-11  RELIANCE     EQ     1107.50  1107.60  1113.80  1088.60  1095.50   
2019-01-14  RELIANCE     EQ     1098.05  1095.00  1100.50  1086.40  1099.00   
2019-01-15  RELIANCE     EQ     1096.80  1105.00  1132.00  1105.00  1130.95   
2019-01-16  RELIANCE     EQ     1129.65  1135.00  1145.00  1130.35  1136.50   
2019-01-17  RELIANCE     EQ     1135.90  1144.45  1147.90  1130.00  1133.00   
2019-01-18  RELIANCE     EQ     1134.45  1148.80  1189.90  1135.25  1185.80   
2019-01-21  RELIANCE     EQ     1184.35  1194.00  1239.95  1188.65  1235.00   
2019-01-22  RELIANCE     EQ     1237.70  1232.85  1246.95  1219.60  1232.00   
2019-01-23  RELIANCE     EQ     1235.15  1233.30  1244.20  1222.00  1224.45   
2019-01-24  RELIANCE     EQ     1226.30  1225.00  1253.20  1220.10  1249.00   
2019-01-25  RELIANCE     EQ     1247.45  1250.45  1264.70  1235.40  1247.10   
2019-01-28  RELIANCE     EQ     1246.00  1250.50  1255.95  1222.40  1230.50   
2019-01-29  RELIANCE     EQ     1229.55  1231.00  1231.65  1201.35  1214.90   
2019-01-30  RELIANCE     EQ     1210.65  1215.00  1225.00  1191.10  1195.75 

In the above dataframe, I can clearly see the Date column as a part of my dataframe, but when I write:

print(df.Date)

I get the below error:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    df.Date
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 3614, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'Date'

I am not able to understand what is the problem.

Upvotes: 0

Views: 901

Answers (1)

Erfan
Erfan

Reputation: 42916

You have to reset your index, to get it back as a column you can work with.

df.reset_index(inplace=True)

Or access the index itself

print(df.index)

Upvotes: 2

Related Questions