繁体   English   中英

读取 header 包含 Python pandas 中的日期

[英]Reading header containing dates in Python pandas

我有一张 excel 表:

  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%

需要明确的是,问题不在于读取文件,而在于下面的问题。

我想在 python 中使用 pandas 读取此文件,并将 header 中的日期作为字符串 这样以后我就可以使用 df['31-12-2019'] 之类的内容来引用任何列。

当我现在阅读 excel 时,我收到一条 keyerror 消息,因为 header 中的日期格式已更改。 我现在是这样读的:

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

选择例如列 31-12-2019 时收到错误消息:“Keyerror:'31-12-2019'。任何帮助将不胜感激!

另外,第一列没有 header,我自己如何将其命名为“年”?

当我使用它时它起作用了:

import pandas as pnd

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

我不知道如何命名标题...

我通过阅读以下文件来解决我的问题:

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

然后到 select 例如我使用的第 91 列 .loc (因为不推荐使用 .ix ),我通过以下方式做到了:

M12 = curve.loc[:, 91]

希望对其他人也有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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