Pandas Dataframe 提供了更改列值數據類型的自由。我們可以將它們從 Integers 更改為 Float 類型,將 Integer 更改為 Datetime,String 更改為 Integer,Float 更改為 Datetime 等。為了將 float 轉換為 DateTime,我們使用 pandas.to_datetime() 函數並使用以下語法:
Syntax:pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin=’unix’, cache=False)
範例1:使用 pandas.to_datetime() 將一列從 float 轉換為 ‘yyyymmdd’ 格式
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list
# with Data set
player_list = [[20200112.0,'Mathematics'],
[20200114.0,'English'],
[20200116.0,'Physics'],
[20200119.0,'Chemistry'],
[20200121.0,'French'],
[20200124.0,'Biology'],
[20200129.0,'Sanskrit']]
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
# printing dataframe
print(df)
print()
# checking the type
print(df.dtypes)
輸出:
更改數據類型後。
Python3
# converting the float to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d')
# printing dataframe
print(df)
print()
print(df.dtypes)
輸出:
在上麵的示例中,我們將“Dates”列的數據類型從“float64”更改為“datetime64[ns]”類型。
範例2:如果 DataFrame 列是 yymmdd 格式,我們必須將其轉換為 yyyymmdd 格式
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with
# Data set
player_list = [[180112.0,'Mathematics'],
[180114.0,'English'],
[180116.0,'Physics'],
[180119.0,'Chemistry'],
[180121.0,'French'],
[180124.0,'Biology'],
[180129.0,'Sanskrit']]
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
# printing dataframe
print(df)
print()
# checking the type
print(df.dtypes)
輸出:
更改數據類型後。
Python3
# converting the float to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%y%m%d')
# printing dataframe
print(df)
print()
print(df.dtypes)
輸出:
在上麵的示例中,我們將“日期”列的數據類型從“float64”更改為“datetime64[ns]”,並將格式從“yymmdd”更改為“yyyymmdd”。
範例3:當我們必須將浮點列轉換為日期和時間格式時
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [[20200112082520.0,'Mathematics'],
[20200114085020.0,'English'],
[20200116093529.0,'Physics'],
[20200119101530.0,'Chemistry'],
[20200121104060.0,'French'],
[20200124113541.0,'Biology'],
[20200129125023.0,'Sanskrit']]
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Dates','Test'])
# printing dataframe
print(df)
print()
# checking the type
print(df.dtypes)
輸出:
更改數據類型後。
Python3
# converting the float to datetime format
df['Dates'] = pd.to_datetime(df['Dates'], format='%Y%m%d%H%M%S')
# printing dataframe
print(df)
print()
print(df.dtypes)
輸出:
在上麵的示例中,我們將“Dates”列的數據類型從“float64”更改為“datetime64[ns]”,並將格式更改為日期和時間
範例4:使用 pandas.to_datetime() 將多列從 float 轉換為“yyyymmdd”格式
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [[20200112.0,'Mathematics',20200113.0],
[20200114.0,'English',20200115.0],
[20200116.0,'Physics',20200117.0],
[20200119.0,'Chemistry',20200120.0],
[20200121.0,'French',20200122.0],
[20200124.0,'Biology',20200125.0],
[20200129.0,'Sanskrit',20200130.0]]
# creating a pandas dataframe
df = pd.DataFrame(player_list,columns=['Starting_Date','Test','Ending_Date'])
# printing dataframe
print(df)
print()
# checking the type
print(df.dtypes)
輸出:
更改數據類型後。
Python3
# converting the float to datetime format
# in multiple columns
df['Starting_Date'] = pd.to_datetime(df['Starting_Date'],
format='%Y%m%d')
df['Ending_Date'] = pd.to_datetime(df['Ending_Date'],
format='%Y%m%d')
# printing dataframe
print(df)
print()
print(df.dtypes)
輸出:
在上麵的示例中,我們將“Starting_Date”和“Ending_Date”列的數據類型從“float64”更改為“datetime64[ns]”類型。
相關用法
- Pandas DataFrame Integer轉Datetime用法及代碼示例
- Python Pandas DataFrame.fillna()用法及代碼示例
- Pandas DataFrame Index轉Column用法及代碼示例
- Pandas DataFrame String轉Integer用法及代碼示例
注:本文由純淨天空篩選整理自vanshgaur14866大神的英文原創作品 How to Convert Float to Datetime in Pandas DataFrame?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。