Home > Net >  Cant create index with dataframe columns
Cant create index with dataframe columns

Time:12-09

I'm trying to use dataframe columns to create an index.

#columns in file [<TICKER>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>]
df = pd.read_csv('DSKY_210101_211106.csv', header=0, parse_dates=[2, 3])
#projection
project = df[['<DATE>', '<TIME>', '<HIGH>']]
project.set_index(['<DATE>','<TIME>'])
print(project)

But the index still the same. Why didn't columns become index ?

enter image description here

CodePudding user response:

pandas.DataFrame.set_index() is not in-place, i.e., it creates a new dataframe and modifies that. You need to reassign to project:

project = project.set_index(['<DATE>','<TIME>'])

CodePudding user response:

Adding to @user17242583's answer pd.Dataframe.set_index is not inplace unless specified in its arguments:

#columns in file [<TICKER>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>]
df = pd.read_csv('DSKY_210101_211106.csv', header=0, parse_dates=[2, 3])
#projection
project = df[['<DATE>', '<TIME>', '<HIGH>']]
project.set_index(['<DATE>','<TIME>'], inplace=True)
print(project)
  • Related