简体   繁体   中英

Reading header containing dates in Python pandas

I have an excel sheet:

  31-12-2019  31-01-2020  28-02-2020 *(which btw is formatted as: 31-Dec-19, 31-Jan-20, etc. not sure if relevant)* 
1  -0,36%       0,12%      -0,09%
2  -0,18%       0,06%      -0,07%
3   0,05%       0,04%       0,14%

To be clear, the problem is not in reading the file, but the issue below.

I want to read this file with pandas in python and have the dates in the header as strings . So that later i can to refer to any column with something like df['31-12-2019'].

When I read the excel now, I get a keyerror message, because the formats of the dates in the header are changed. I read it like this now:

curve = pd.read_excel("Monthly curves.xlsx", sheet_name = "swap", skiprows = 1, index_col = 0)

I receive the error when selecting for instance column 31-12-2019: "Keyerror: '31-12-2019'. Any help would be much appreciated!

Also, the first column does not have a header, how can I name it myself as 'years'?

It worked when I used this:

import pandas as pnd

file = 'excelfile.xlsx'
df = pnd.read_excel(file,sheet_name=0,index_col=0)
df.head()

I don't know about naming the headers though...

I worked around my problem by reading the file as follows:

curve = pd.read_excel("Monthly Curves.xlsx", sheet_name = "swap", index_col = 0, skiprows = 2, header = None)

Then to select for instance the 91th column I used.loc (because.ix is deprecated), and I did that in the following way:

M12 = curve.loc[:, 91]

Hope that helps others as well!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM