简体   繁体   English

数据框列中的日期转换为MM / DD / YYYY

[英]Conversion of dates in the dataframe column into MM/DD/YYYY

I have 4 dataframes named df1 , df2 , df3 , df4 with one column Date . 我有4个名为df1df2df3df4数据df1 ,其中有一列Date The column consist of dates with different format. 该列由不同格式的日期组成。 df1 has date column of type int64, df2 has date column of type object, df3 has date column has type object, df4 has date column of type object. df1日期列的类型为int64, df2日期列的类型为object, df3日期列的类型为object, df4日期列的类型为object。 Below are the dataframe and its data. 以下是数据框及其数据。

df1:
          Date
0     20160301
1     20160301
2     20160301
3     20160301

df2: 
      Date
0   01/03/2016
1   01/03/2016
2   01/03/2016

df3:
      Date
0   31-Mar-16
1   31-Mar-16
2   31-Mar-16

df4:
      Date
0  25/02/2016
1  25/02/2016
2  25/02/2016

I want to convert these dates in the form as mm/dd/yyyy of type date . 我想将这些日期转换为date类型的mm/dd/yyyy形式。 Can anyone help me on this? 谁可以帮我这个事?

I think you can use to_datetime and dt.strftime , but type is not datetime , but string : 我认为您可以使用to_datetimedt.strftime ,但type不是datetime ,而是string

df1['Date'] = pd.to_datetime(df1['Date'], format='%Y%m%d').dt.strftime('%m/%d/%Y')
df2['Date'] = pd.to_datetime(df2['Date']).dt.strftime('%m/%d/%Y')
df3['Date'] = pd.to_datetime(df3['Date']).dt.strftime('%m/%d/%Y')
df4['Date'] = pd.to_datetime(df4['Date']).dt.strftime('%m/%d/%Y')

print df1
print df2
print df3
print df4
         Date
0  03/01/2016
1  03/01/2016
2  03/01/2016
3  03/01/2016
         Date
0  01/03/2016
1  01/03/2016
2  01/03/2016
         Date
0  03/31/2016
1  03/31/2016
2  03/31/2016
         Date
0  02/25/2016
1  02/25/2016
2  02/25/2016

print type(df1.at[0,'Date'])
<type 'str'>

If you want datetime , format is YY-MM-DD : 如果需要datetime ,格式为YY-MM-DD

df1['Date'] = pd.to_datetime(df1['Date'], format='%Y%m%d')
df2['Date'] = pd.to_datetime(df2['Date'])
df3['Date'] = pd.to_datetime(df3['Date'])
df4['Date'] = pd.to_datetime(df4['Date'])

print df1
print df2
print df3
print df4
        Date
0 2016-03-01
1 2016-03-01
2 2016-03-01
3 2016-03-01
        Date
0 2016-01-03
1 2016-01-03
2 2016-01-03
        Date
0 2016-03-31
1 2016-03-31
2 2016-03-31
        Date
0 2016-02-25
1 2016-02-25
2 2016-02-25

print type(df1.at[0,'Date'])
<class 'pandas.tslib.Timestamp'>

More info about formating datetime is here . 有关格式化datetime更多信息在这里

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

相关问题 Python Dataframe 日期列 - 将格式更改为 yyyy-mm-dd - Python Dataframe column of dates - change format to yyyy-mm-dd 如何在 pyspark 数据框中以“DD/MM/YYYY”格式转换日期? - How to convert dates in "DD/MM/YYYY" format in a pyspark dataframe? matplotlib DateFormatter 未在 yyyy-mm-dd 列中显示正确的日期 - matplotlib DateFormatter not showing correct dates with yyyy-mm-dd column 将日期列表从 YYYY/MM/DD 更改为 DD/MM/YYYY - Change list of dates from YYYY/MM/DD to DD/MM/YYYY 将日期时间数据的数据框列转换为 DD/MM/YYYY 字符串数据 - Converting dataframe column of datetime data to DD/MM/YYYY string data 在 Python 中将日期列转换为 dataframe 中的 MM/DD/YYYY 格式 - Convert a date column to MM/DD/YYYY format in a dataframe in Python 日期时间列在 mm/dd/yyyy 中,需要 dd/mm/yyyy - DateTime column coming in mm/dd/yyyy, want dd/mm/yyyy 如何将'dd / mm / yyyy%H:%M'格式的pandas数据帧日期列转换为'yyyy / mm / dd%H:%M' - How to convert pandas dataframe date column in format of 'dd/mm/yyyy %H:%M' to 'yyyy/mm/dd %H:%M' 如何在 dataframe 中将正常日期(dd-mm-yyyy)转换为儒略日期? (Python) - How do you covert normal dates (dd-mm-yyyy) into Julian dates in a dataframe? (Python) Pandas:时间戳转换为 YYYY-MM-DD - Pandas: Timestamp conversion to YYYY-MM-DD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM