突出显示熊猫数据框中的日期格式

时间:2021-05-24 07:00:56

标签: python pandas date

我想在一列中突出显示熊猫数据框中的所有日期。

索引A

0 24

1 32-35

2 10/01/2016

3 02/20/2017

4 02/20/2017

5 02/20/2017

1 个答案:

答案 0 :(得分:1)

想法是测试 datetime 是否将值转换为 to_datetime 并测试是否缺少值,因为 errors='coerce' 没有日期时间返回 NaN

def color_datetimes(val):

    color = 'red' if pd.notna(pd.to_datetime(val, errors='coerce')) else 'black'
    return 'color: %s' % color


df.style.applymap(color_datetimes, subset=['A']).to_excel('file.xlsx', index=False)