【问题标题】:How to get rid of timestamps infront of Date, as Pandas adds time stamps to date columns after saving to excel如何摆脱 Date 前面的时间戳,因为 Pandas 在保存到 Excel 后将时间戳添加到日期列
【发布时间】:2021-09-03 23:48:51
【问题描述】:

我是一名学生,我正在学习熊猫。

我创建了名为 Student_Record.xlsx 的 excel 文件(使用 microsoft excel)

我想用 pandas 创建新文件

import pandas as pd
df = pd.read_excel(r"C:\Users\sudarshan\Desktop\Student_Record.xlsx")
df.head()
df.to_excel(r"C:\Users\sudarshan\Desktop\Output.xlsx",index=False)

我在 pandas 中打开了文件,然后用不同的名称将文件保存回 excel(文件名 = 输出)

我将文件保存回 Excel,但是当我在 MS.Excel 上打开文件(输出)时,列(DOB 和 YOP)附有日期的时间戳。

请告诉我如何只打印日期?(我希望输出文件及其内容与原始文件完全相同)

希望得到一些帮助/支持。 谢谢

【问题讨论】:

标签: python excel pandas datetime


【解决方案1】:

您的DOB 和Year of passing 列在保存到Excel 之前可能是datetime 格式。结果,它们在保存到 Excel 时被转换回日期时间表示。

如果您想保留其内容与dd-mm-YYYY 格式的原始文件完全相同,您可以尝试将这两列转换为字符串格式,然后再保存到 Excel。你可以这样做:

df['DOB'] = df['DOB'].dt.strftime('%d-%m-%Y')
df['Year of passing'] = df['Year of passing'].dt.strftime('%d-%m-%Y')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2011-09-02
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多