Python 更改列日期格式
我在pandas dataframe中有如下列:Python 更改列日期格式,python,python-3.x,pandas,Python,Python 3.x,Pandas,我在pandas dataframe中有如下列: 0 2018-04-06 1 2018-04-06 2 2018-04-09 3 2018-04-19 4 2018-04-19 5 2018-04-17 def change_date_format(x): if x != 'nan' and x != '' and x != ' ' and x != 0: x = parse(x, dayfirst=True).strftime("
0 2018-04-06
1 2018-04-06
2 2018-04-09
3 2018-04-19
4 2018-04-19
5 2018-04-17
def change_date_format(x):
if x != 'nan' and x != '' and x != ' ' and x != 0:
x = parse(x, dayfirst=True).strftime("%Y-%m-%d")
return x
else:
return ''
read4['Column Name'] = read4['Column Name'].apply(lambda x : change_date_format(x) )
2018-06-04
2018-06-04
2018-09-04
2018-04-19
2018-04-19
2018-04-17
我想将此列转换为yyyy/mm/dd,为此我提供了如下指导:
0 2018-04-06
1 2018-04-06
2 2018-04-09
3 2018-04-19
4 2018-04-19
5 2018-04-17
def change_date_format(x):
if x != 'nan' and x != '' and x != ' ' and x != 0:
x = parse(x, dayfirst=True).strftime("%Y-%m-%d")
return x
else:
return ''
read4['Column Name'] = read4['Column Name'].apply(lambda x : change_date_format(x) )
2018-06-04
2018-06-04
2018-09-04
2018-04-19
2018-04-19
2018-04-17
但它的转换如下:
0 2018-04-06
1 2018-04-06
2 2018-04-09
3 2018-04-19
4 2018-04-19
5 2018-04-17
def change_date_format(x):
if x != 'nan' and x != '' and x != ' ' and x != 0:
x = parse(x, dayfirst=True).strftime("%Y-%m-%d")
return x
else:
return ''
read4['Column Name'] = read4['Column Name'].apply(lambda x : change_date_format(x) )
2018-06-04
2018-06-04
2018-09-04
2018-04-19
2018-04-19
2018-04-17
理想情况下应该是:
2018-04-06
2018-04-06
2018-04-09
2018-04-19
2018-04-19
2018-04-17
我如何强制它执行上述工作。基本上它应该考虑输入也取决于它应该工作。 < P>我认为需要参数<代码>错误=“强制”< /代码>将不可解析值转换为<代码> NAT < /代码>,然后最后:
也许您需要将
dayfirst
标志设置为False
x = parse(x, dayfirst=False).strftime("%Y-%m-%d")
我知道这很好,但只适用于此场景,不适用于其他场景;此对话已结束。brother我是否可以为
提供多个单位
到_datetime
,因为我需要将np.int64
类型列转换为不同范围的整数。@piyuss.Wanare-您能解释更多吗?也许最好用样本数据和预期结果来创建新问题。