Python 正在添加年初至今格式%a,%d/%m
我正试图从这个网站上抓取一张桌子:Python 正在添加年初至今格式%a,%d/%m,python,python-3.x,pandas,Python,Python 3.x,Pandas,我正试图从这个网站上抓取一张桌子: table=pd.read\u html(“https://www.epexspot.com/en/market-data/capacitymarket“”[0] 以下是它给出的输出: 我想将列更改为格式%y-%m-%d。上表的列应为 2018-09-132018-10-182018-12-132019-03-212019-05-1522019-06-2722019-09-12 有什么建议吗 通过迭代表.列并使用日期时间模块。只需确保使用replace(年
table=pd.read\u html(“https://www.epexspot.com/en/market-data/capacitymarket“”[0]
以下是它给出的输出:
我想将列更改为格式%y-%m-%d。上表的列应为
2018-09-132018-10-182018-12-132019-03-212019-05-1522019-06-2722019-09-12
有什么建议吗 通过迭代
表.列
并使用日期时间
模块。只需确保使用replace(年份=2019)
,否则将使用默认年份(1900)
from datetime import datetime
table.columns = [datetime.strptime(column, '%a, %d/%m').replace(year=2019).strftime('%Y-%m-%d')
if '/' in column else column for column in table.columns]
如果您不喜欢长列表,可以使用map
:
def rename_col(col):
if '/' in col:
return datetime.strptime(col, '%a, %d/%m').replace(year=2019).strftime('%Y-%m-%d')
return col
table.columns = map(rename_col, table.columns)
原始表数据是什么?我无法访问主页。@Lamanus您可以访问此页面吗?哦,你想更改标题格式而不是列,对吗?@Lamanus是的,我会在我的问题中编辑。我在列中有2018年和2019年。“Thu,13/12”是过渡年的标志