简体   繁体   English

如何在 python 的系列中逐月交换?

[英]How to swap day by month in a Series with python?

I have a column in which there are dates:我有一列有日期:

df['Date']

    Date
0 2020-25-04
1 2020-26-04
2 2020-27-04
3 2020-12-05
4 2020-06-05
Name: Date, Length: 5, dtype: datetime64[ns]

I want to swap the element Day by element Month, so I can have:我想逐个元素月交换元素日,所以我可以拥有:

df['Date']

     Date
0 2020-04-25
1 2020-04-26
2 2020-04-27
3 2020-05-12
4 2020-05-06
Name: Date, Length: 5, dtype: datetime64[ns]

Any help would be appreciated.任何帮助,将不胜感激。

import pandas as pd
import numpy as np

df = pd.DataFrame({'Date':[np.datetime64('2020-04-25') ,np.datetime64('2020-04-26')]})

df['Date'] = df['Date'].apply(lambda x: x.strftime('%Y-%m-%d'))

print(df)

I converted data into np.datetime format and applied lambda function.我将数据转换为 np.datetime 格式并应用 lambda function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM