[英]Separate date and time from csv column
I have a CSV file which has a column datetime in this format:我有一个 CSV 文件,其中包含以下格式的日期时间列:
|datetime | dataA |dataB|
---------------------------
|211201000000 | 1 | 222
|211201000000 | 11|221
|211201000000 | 12|111
|211201000000 | 11|144
|211201000000 | 13|1122
I want to write datetime column in this format(format='%Y-%m-%d %H:%M:%S'):我想用这种格式写日期时间列(格式='%Y-%m-%d %H:%M:%S'):
2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00
I am using this line to change my datetime column:我正在使用这一行来更改我的日期时间列:
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H:%M:%S')
or或者
df['datetime'] = pd.to_datetime(df['datetime']).dt.date
Output I get in datetime column from above lines are: Output 我从上面的行中得到的日期时间列是:
1970-01-01 }
.........
........
which are not even in CSV columns.这甚至不在 CSV 列中。
But these lines don't change my date time as I expect.但是这些行并没有像我预期的那样改变我的日期时间。
How can I change them in to my desire format so that I can use it easily in further work?如何将它们更改为我想要的格式,以便我可以在进一步的工作中轻松使用它?
The format you need to use is %y%m%d%H%M%S
.您需要使用的格式是
%y%m%d%H%M%S
。 A short example:一个简短的例子:
from datetime import datetime as dt
s = "211201000000"
dt.strptime(s, "%y%m%d%H%M%S")
OUTPUT OUTPUT
datetime.datetime(2021, 12, 1, 0, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.