gpt4 book ai didi

python - 使用 Pandas 将字符串转换为日期时间

转载 作者:行者123 更新时间:2023-12-01 03:20:45 26 4
gpt4 key购买 nike

我有一列包含字符串作为日期时间,我正在尝试将迄今为止的专栏。尽管该代码片段不会抛出错误,但它会返回零(00:00:00),我请求您指导如何将“周”列转换为 MM/dd/yyyy 日期格式。

import pandas as pd
import csv
from pandas import DataFrame

third = pd.read_csv('C:/Users/emailcombo.csv')
third = third.rename(columns={'Phone Number:': 'Phone Number:'})

fourth = pd.read_csv('C:/Users/The_Real_InfoCenter.csv')
fourth = fourth.rename(columns={'Phone Number: ': 'Phone Number:'})

merged = pd.concat([third, fourth], ignore_index = True)

df = DataFrame(merged)
df['Week:'] = pd.to_datetime(df['Week:'])
df.sort_values(['Week:'], ascending = [False])

df.to_csv('Z:/AWS/updatedinfocentertracker.csv', index=False)

print "Updating has been done"

最佳答案

我建议您提供一部分数据。试试这个:

import pandas as pd
df['Week:'] = pd.Series([pd.to_datetime(d) for d in df['Week:']])
df.sort_values(['Week:'], ascending = [False])

关于python - 使用 Pandas 将字符串转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com