![](/img/trans.png)
[英]pandas does not read a excel file fine after I've save a Datetime cell
[英]Why does Python Pandas read the string of an excel file as datetime
正如@ddejohn 在评论中正确地说的那样,您面临的行为实际上来自 Excel,自动将数据转换为日期。 因此 pandas 将不得不处理该数据作为日期,并在以后处理它以获得正确的格式,就像你说你不能修改输入 Excel 文件一样。
这是一个简短的脚本,可以使其按预期工作:
import pandas as pd
def rev(x: str) -> str:
'''
converts '2003-02-14 00:00:00' to '14.02.03'
'''
hours = '00:00:00'
if not hours in x:
return x
y = x.split()[0]
y = y.split('-')
return '.'.join([i[-2:] for i in y[::-1]])
file = r'C:\your\folder\path\Classeur1.xlsx'
df = pd.read_excel(file, dtype=str)
df['column'] = df['column'].apply(rev)
将df['column']
替换为您的实际列名。 然后您在 dataframe 中获得所需的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.