0

I am importing the data with this command

df = pd.read_excel('C:/Users/Me/Data.xlsx', sheet_name='Prices')

and this is the result:

1

The date is a common column and I want it like this:

2

  • Hi! Please don’t post images of the data as we can’t test them. Instead, post a sample of the DataFrame and expected output directly inside a code block. This allows us to easily reproduce your problem and help you. Take the time to read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and revise your question accordingly. – Rodalm May 14 '22 at 22:58

3 Answers3

1

I found the answer.Adding parse_dates=True, index_col=0 to the import command like this:

df = pd.read_excel('C:/Users/Me/Data.xlsx', sheet_name='Prices', parse_dates=True, index_col=0)

The output is this:

1

Rodalm
  • 5,169
  • 5
  • 21
0

What you are trying to do is to set Date as an index, if I get it right:

df.set_index('Date')
Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15
  • This answer should go as a comment, really, but I don't have yet enough reputation... Maybe you want to edit the question so to make clear you are asking for an index, if this is what you were asking for. – Ignatius Reilly May 14 '22 at 22:29
0

I found a better way to import them, because when I was trying to calculate the monthly returns it does not work. So I use this new code and the date were perfect.

df = pd.read_excel('C:/Users/Me/Data.xlsx', sheet_name='Prices')
df.index = pd.to_datetime(df['Date'])
df.drop(['Date'], axis = 'columns', inplace=True)
df.head()

and this is the result:

1