KeyError while setting index of Dataframe

I have a dataframe port

and I am trying to set its index as follows. However, I am getting an error KeyError: 0

on the line port = port.set_index(l)

and am not sure why this is the case. It currently port

has a row index.


as follows:

Date            ^GSPC       AAPL         IBM      dell        wmt                                                   
2013-10-29  1771.949951  71.290195  174.302136  13.86000  73.328899
2013-10-28  1762.109985  73.111495  169.736908  13.83000  73.405027



l = [0]*port.shape[0]
count = 0
for i,j in enumerate(l):
    l[i] = count
    count += 1 
port = port.set_index(l)




source to share

1 answer


sets the DataFrame index (row labels) using one or more existing columns, keys

is a column label or a list of column / array labels.

I think you want to set the index of the DataFrame like l

? You can set it to

port = port.set_index([l])


or simply

port.index = l




All Articles