如何在Pandas数据框架中把整数转换成浮点数
Pandas Dataframe提供了改变列值的数据类型的自由。我们可以把它们从整数改为浮点数类型,整数改为字符串,字符串改为整数,等等。
有2种方法可以将整数转换为浮点数。
方法1:使用DataFrame.astype()方法
语法 :
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
例子1:使用DataFrame.astype()将个列从int转换为float。
# 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)
输出:
让我们把重量类型转换为浮点
# 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。
# 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)
输出:
让我们把年龄和罢工率转换为浮点数类型
# 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
输出:
在上面的例子中,我们将列 “年龄 “和 “打击率 “的数据类型从 “int64 “改为 “float64″。
方法2:使用pandas.to_numeric()方法
语法 :
pandas.to_numeric(arg, errors=’raise’, downcast=None)
例子1:使用pandas.to_numeric()将一个**列从int转换为float。
# 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
# 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:
# 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
# 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″。