本文介绍了将一列日期从序号转换为标准日期格式-Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一列日期从整数/日期格式转换为日期格式d-m-Y.示例:

I have to convert a column of dates from the integer/date format to the date format d-m-Y. Example:

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

推荐答案

datetime fromordinal 应该会有所帮助.

datetime fromordinal should help.

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)

这篇关于将一列日期从序号转换为标准日期格式-Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:32