Миша Попов
Миша Попов

Reputation: 137

Cant create index with dataframe columns

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

Upvotes: 0

Views: 170

Answers (3)

sudeep garg
sudeep garg

Reputation: 1

Please use the set_index with inplace= True

Upvotes: -1

Learning is a mess
Learning is a mess

Reputation: 8277

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)

Upvotes: 0

user17242583
user17242583

Reputation:

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>'])

Upvotes: 2

Related Questions