0

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

2
  • 2
    set_index by default creates a new df object, if you want it to change the existing one set inplace=true you can see this in the docs pandas.pydata.org/docs/reference/api/… inplacebool, default False If True, modifies the DataFrame in place (do not create a new object). Commented Dec 8, 2021 at 16:27
  • 1
    Does this answer your question? Dataframe set_index not setting Commented Dec 8, 2021 at 16:50

3 Answers 3

2

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>'])
Sign up to request clarification or add additional context in comments.

8 Comments

This is a duplicate. There's no need to answer a question that has been already asked and answered elsewhere. Please first look for duplicates before answering questions, and vote to close as duplicate when appropriate, thanks!
Oh really? Ok, thank you for find it. I didn't know that there was. I'll save it. :)
(In other words, there are many many duplicates in the pandas tag)
Fair enough! My rule of thumb: if it's a basic pandas question, it's been asked (probably many times) already on this site :D
Jezrael's profile has a convenient list haha
|
0

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)

Comments

-1

Please use the set_index with inplace= True

2 Comments

This adds nothing but noise to the thread, which is already a duplicate. Please don't do that.
This does not add anything to the thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.