0

I've extracted data from a webpage by using pandas read_csv command. After extracting data I removed the first to row and converted column indexing. But when I try to convert index to date column I get an error every time. Following is the code.

    df=df.ix[2: , ]
    df.columns=['Date','Open','High','Low','Close','Volume']
    #df=pd.DataFrame(df,index=df.ix[:,0])
    #df=pd.DataFrame(df,index='Date')
    #df=pd.DataFrame(df,index=df.ix[:,0], usecols=['Date','Open','High','Low','Close','Volume']

I need a way out to make Date column as an index of DataFrame.

2 Answers 2

2
df.set_index('Date', inplace=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Solved!
1

In case someone else finds this seeking for a solution for a similar problem: When using pandas read_csv command you can specify the column that will be used as index using the index_col method.

In the example below, the first column will be used as index:

df = pd.read_csv(filepath, index_col = 0)

Comments

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.