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?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。