import pandas as pd
df1 = pd.read_csv('sales.csv')
df2 = pd.read_csv('sales1.csv')
df3 = pd.read_csv('sales2.csv')
df4 = pd.read_csv('sales3.csv')
df=pd.concat([df1,df2,df3,df4])
df['Transaction date']
给我以下输出:
0 01/05/2018
1 30/04/2018
2 30/04/2018
3 25/04/2018
4 23/04/2018
5 23/04/2018
6 22/04/2018
7 22/04/2018
8 22/04/2018
9 21/04/2018
10 18/04/2018
11 18/04/2018
12 17/04/2018
13 17/04/2018
14 16/04/2018
15 15/04/2018
16 15/04/2018
17 15/04/2018
18 14/04/2018
19 14/04/2018
20 14/04/2018
21 13/04/2018
22 13/04/2018
23 13/04/2018
24 11/04/2018
25 11/04/2018
26 10/04/2018
27 09/04/2018
28 09/04/2018
29 07/04/2018
当我使用pd.to_datetime()
时,某些月份和日期会发生变化:
import pandas as pd
df1 = pd.read_csv('sales.csv')
df2 = pd.read_csv('sales1.csv')
df3 = pd.read_csv('sales2.csv')
df4 = pd.read_csv('sales3.csv')
df=pd.concat([df1,df2,df3,df4])
df['Transaction date'] = pd.to_datetime(df['Transaction date'])
df['Transaction date']
0 2018-01-05
1 2018-04-30
2 2018-04-30
3 2018-04-25
4 2018-04-23
5 2018-04-23
6 2018-04-22
7 2018-04-22
8 2018-04-22
9 2018-04-21
10 2018-04-18
11 2018-04-18
12 2018-04-17
13 2018-04-17
14 2018-04-16
15 2018-04-15
16 2018-04-15
17 2018-04-15
18 2018-04-14
19 2018-04-14
20 2018-04-14
21 2018-04-13
22 2018-04-13
23 2018-04-13
24 2018-11-04
25 2018-11-04
26 2018-10-04
27 2018-09-04
28 2018-09-04
29 2018-07-04
从输出24到29月份和日期切换。我想将列Transaction date
更改为datetime64[ns]
类型,但希望我的日期格式为yyyy/mm/dd
。
以上格式为:dd/mm/yyyy
,pd.to_datetime()
格式应更改为yyyy-mm-dd
。这适用于输出0到23,但是从输出24到29,它从dd/mm/yyyy
变为yyyy-dd-mm
。我希望datetime64[ns]
的所有日期都采用相同的格式yyyy-mm-dd
。
我找不到解决办法,有人可以帮帮我吗?
答案 0 :(得分:0)
如果您定义格式似乎已解决:
pd.to_datetime(df['Transaction date'], format = '%d/%m/%Y')