📌  相关文章
📜  如何在 Pandas DataFrame 中将整数转换为浮点数?

📅  最后修改于: 2022-05-13 01:54:56.423000             🧑  作者: Mango

如何在 Pandas DataFrame 中将整数转换为浮点数?

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


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)


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


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)


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


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)


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


输出:

让我们将重量类型转换为浮点数

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

输出:

在上面的示例中,我们将列“ Weight ”的数据类型从“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转换为float类型

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 

输出:

在上面的示例中,我们将列“ Weight ”的数据类型从“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

输出:

在上面的示例中,我们将“Experience”和“Height”列的数据类型从“int64”更改为“float32”。