如何将不同格式的日期时间列转换为特定格式?
嗨,我不是 python 专家,我仍然是使用 pandas 和处理数据的初学者。 我有一个带有列时间戳的 df 。列中的日期时间如下所示:
2021-09-07 16:36:14 UTC
2021-09-04 15:31:44 UTC
2021-07-15 06:49:47.320081 UTC
2021-09-07 14:55:55.353145 UTC
我只想包含日期和时间,末尾没有 UTC 文本,秒后没有小数,最后将数据帧保存在 csv 文件< /强>。基本上我想要这种格式的专栏:
2021-09-07 16:36:14
2021-09-04 15:31:44
2021-07-15 06:49:47
2021-09-07 14:55:55
我尝试使用这两个函数:
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')
我解决了一半的问题。秒后没有小数的日期时间得到修复,但有小数的日期时间只是变空,您可以找到下面的示例:
2021-09-07 16:36:14
2021-09-04 15:31:44
请问有人可以帮我解决这个问题吗?
Hi I am not an expert in python and I am still a beginner in using pandas and working with data.
I have a df with a column timestamp. The datetime in the column are as shown below:
2021-09-07 16:36:14 UTC
2021-09-04 15:31:44 UTC
2021-07-15 06:49:47.320081 UTC
2021-09-07 14:55:55.353145 UTC
I would like to have only the date and time, without the UTC text at the end and without the decimals after the second and in the end save the dataframe in a csv file. Basically I want the column in this format:
2021-09-07 16:36:14
2021-09-04 15:31:44
2021-07-15 06:49:47
2021-09-07 14:55:55
I tried with these two functions:
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')
I fix half of the problem. The datetime without the decimals after the second get fixed, but the ones with the decimals just get empty, you can find the example below:
2021-09-07 16:36:14
2021-09-04 15:31:44
Please can someone help me with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试提取您想要的字段部分。
Try extracting the part of the field you want.
你可以取前20个字符:
如果你想保留时区信息(UTC),你可以只删除微秒部分:
You can take the first 20 characters:
If you want to keep the timezone information (UTC), you can remove only the microsecond part:
尝试
parser
,因为它可以采用不同的格式作为输入或此输出
Try
parser
, as it can take different formats as an inputOr this output