我必须将一列日期从整数/日期格式转换为日期格式d-m-Y。示例:
import pandas as pd
col1 = [737346, 737346, 737346, 737346, 737059, 737346]
col2 = ['cod1', 'cod2', 'cod3', 'cod4', 'cod1', 'cod2']
dict = {'V1' : col1, 'V2' : col2}
df = pd.DataFrame.from_dict(dict)
df
V1 V2
0 737346 cod1
1 737346 cod2
2 737346 cod3
3 737346 cod4
4 737059 cod1
5 737346 cod2
预期:
df
V1 V2
0 14-10-2019 cod1
1 14-10-2019 cod2
2 14-10-2019 cod3
3 14-10-2019 cod4
4 31-12-2018 cod1
5 14-10-2019 cod2
答案 0 :(得分:5)
日期时间fromordinal
应该会有所帮助。
import datetime as dt
col1 = [737346, 737346, 737346, 737346, 737059, 737346]
col2 = ['cod1', 'cod2', 'cod3', 'cod4', 'cod1', 'cod2']
dd = {'V1' : col1, 'V2' : col2}
df = pd.DataFrame.from_dict(dd)
df['V1'] = df['V1'].apply(dt.datetime.fromordinal)
答案 1 :(得分:2)
JObject
pandas
Timestamp.fromordinal
答案 2 :(得分:0)
您可以为此使用date.fromordinal
。
from datetime import datetime as dt
df['V1'] = df.V1.apply(lambda x: dt.fromordinal(x)).dt.strftime('%d-%m-%Y')
print(df)
V1 V2
0 14-10-2019 cod1
1 14-10-2019 cod2
2 14-10-2019 cod3
3 14-10-2019 cod4
4 31-12-2018 cod1
5 14-10-2019 cod2