Pandas Dataframe 提供了更改列值數據類型的自由。我們可以將它們從 Integers 更改為 Float 類型,Integer 更改為 String,String 更改為 Integer 等。
有兩種方法可以將整數轉換為浮點數:
方法一:使用DataFrame.astype()方法
用法:
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
範例1:使用 DataFrame.astype() 將一列從 int 轉換為 float
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Strike_rate'])
# lets find out the data type
# of 'Weight' column
print(df.dtypes)
輸出:
讓我們將重量類型轉換為浮點數
Python3
# Now we will convert it from 'int' to 'float' type
# using DataFrame.astype() function
df['Weight'] = df['Weight'].astype(float)
print()
# lets find out the data type after changing
print(df.dtypes)
# print dataframe.
df
輸出:
在上麵的示例中,我們將“重量”列的數據類型從 ‘int64’ 更改為 ‘float64’。
範例2:使用 DataFrame.astype() 將多於一列從 int 轉換為 float
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Strike_rate'])
# lets find out the data type of 'Age'
# and 'Strike_rate' columns
print(df.dtypes)
輸出:
讓我們將 age 和 strike_rate 轉換為浮點類型
Python3
# now Pass a dictionary to astype() function
# which contains two columns
# and hence convert them from int to float type
df = df.astype({"Age":'float', "Strike_rate":'float'})
print()
# lets find out the data type after changing
print(df.dtypes)
# print dataframe.
df
輸出:
在上麵的示例中,我們將“Age”和“Strike_rate”列的數據類型從 ‘int64’ 更改為 ‘float64’。
方法二:使用 pandas.to_numeric() 方法
用法:
pandas.to_numeric(arg, errors=’raise’, downcast=None)
範例1:使用 pandas.to_numeric() 將單列從 int 轉換為 float
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176], 4
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=[
'Name', 'Age', 'Weight', 'Salary', 'Height'])
# lets find out the data type of
# 'Weight' column
print(df.dtypes)
輸出:
讓我們將重量從 int 轉換為 float
Python3
# Now we will convert it from 'int' to 'float' type
# using pandas.to_numeric()
df['Weight'] = pd.to_numeric(df['Weight'], downcast='float')
print()
# lets find out the data type after changing
print(df.dtypes)
# print dataframe.
df
輸出:
在上麵的示例中,我們將“重量”列的數據類型從 ‘int64’ 更改為 ‘float32’。
範例2:
Python3
# importing pandas library
import pandas as pd
# Initializing the nested list with Data set
player_list = [['M.S.Dhoni', 36, 75, 5428000, 176],
['A.B.D Villers', 38, 74, 3428000, 175],
['V.Kholi', 31, 70, 8428000, 172],
['S.Smith', 34, 80, 4428000, 180],
['C.Gayle', 40, 100, 4528000, 200],
['J.Root', 33, 72, 7028000, 170],
['K.Peterson', 42, 85, 2528000, 190]]
# creating a pandas dataframe
df = pd.DataFrame(player_list, columns=[
'Name', 'Experience', 'Weight', 'Salary', 'Height'])
# lets find out the data type of
# 'Experience' and 'Height' columns
print(df.dtypes)
輸出:
讓我們將經驗和高度從 int 轉換為 float
Python3
# Now we will convert them from 'int' to 'float' type
# using pandas.to_numeric()
df['Experience'] = pd.to_numeric(df['Experience'], downcast='float')
df['Height'] = pd.to_numeric(df['Height'], downcast='float')
print()
# lets find out the data type after changing
print(df.dtypes)
# print dataframe.
df
輸出:
在上麵的示例中,我們將“經驗”和“身高”列的數據類型從 ‘int64’ 更改為 ‘float32’。
相關用法
- Pandas DataFrame Strings轉Floats用法及代碼示例
- Pandas DataFrame Floats轉Strings用法及代碼示例
- Pandas DataFrame Integers轉Strings用法及代碼示例
- Python Pandas DataFrame.fillna()用法及代碼示例
- Pandas DataFrame Integer轉Datetime用法及代碼示例
- Pandas DataFrame Index轉Column用法及代碼示例
- Pandas DataFrame String轉Integer用法及代碼示例
注:本文由純淨天空篩選整理自vanshgaur14866大神的英文原創作品 How to Convert Integers to Floats in Pandas DataFrame?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。