正在添加年初至今格式%a,%d/%m

2024-09-29 19:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从这个网站上搜一张桌子: https://www.epexspot.com/en/market-data/capacitymarket/capacity-table/

table = pd.read_html("https://www.epexspot.com/en/market-data/capacitymarket")[0]

它的输出如下: enter image description here

我要将列更改为格式%y-%m-%d。上表的列应为 2018-09-13, 2018-10-18, 2018-12-13, 2019-03-21, 2019-05-15, 2019-06-27, 2019-09-12

有什么建议吗


Tags: httpscomreaddata网站htmlwwwtable
1条回答
网友
1楼 · 发布于 2024-09-29 19:33:19

通过迭代table.columns并使用datetime模块。只需确保使用replace(year=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)

相关问题 更多 >

    热门问题