如何在 Pandas DataFrame 中将字符串转换为浮点数?
在本文中,我们将研究在 pandas 数据帧中将字符串转换为浮点数的不同方法。现在,让我们创建一个以“年份”和“通货膨胀率”为列的数据框。
Python3
# importing pandas library
import pandas as pd
# dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'5.98', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# show the dataframe
print (df)
# show the datatypes
print(df.dtypes)
Python3
# importing pandas library
import pandas as pd
# dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'5.98', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value
# of column to a string
df['Inflation Rate'] = df['Inflation Rate'].astype(float)
# show the dataframe
print(df)
# show the datatypes
print (df.dtypes)
Python3
# importing pandas library
import pandas as pd
# creating a dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'5.98', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value of column to a string
df['Inflation Rate'] = pd.to_numeric(df['Inflation Rate'])
# show the dataframe
print(df)
# show the data types
print (df.dtypes)
Python3
# importing pandas as pd
import pandas as pd
# dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'No data', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value of column to a string
df['Inflation Rate'] = pd.to_numeric(df['Inflation Rate'],
errors = 'coerce')
# show the dataframe
print(df)
# show the data types
print (df.dtypes)
输出:
方法 1:使用DataFrame.astype() 。
该方法用于将 pandas 对象转换为指定的 dtype。
Syntax: DataFrame.astype(self: ~ FrameOrSeries, dtype, copy: bool = True, errors: str = ‘raise’)
Returns: casted: type of caller
示例:在此示例中,我们将“通货膨胀率”列的每个值转换为浮点数。
Python3
# importing pandas library
import pandas as pd
# dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'5.98', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value
# of column to a string
df['Inflation Rate'] = df['Inflation Rate'].astype(float)
# show the dataframe
print(df)
# show the datatypes
print (df.dtypes)
输出:
方法 2:使用pandas.to_numeric()函数。
该函数用于将参数转换为数值类型。
Syntax: pandas.to_numeric(arg, errors=’raise’, downcast=None)
Returns: numeric if parsing succeeded. Note that the return type depends on the input. Series if Series, otherwise ndarray.
示例 1:在此示例中,我们将“通货膨胀率”列的每个值转换为浮点数。
代码:
Python3
# importing pandas library
import pandas as pd
# creating a dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'5.98', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value of column to a string
df['Inflation Rate'] = pd.to_numeric(df['Inflation Rate'])
# show the dataframe
print(df)
# show the data types
print (df.dtypes)
输出:
示例 2:有时,我们可能没有将浮点值表示为字符串。因此,pd.to_numeric()函数将显示错误。为了消除这个错误,我们可以使用errors='coerce' ,将这个位置的值转换为NaN 。
代码:
Python3
# importing pandas as pd
import pandas as pd
# dictionary
Data = {'Year': ['2016', '2017',
'2018', '2019'],
'Inflation Rate': ['4.47', '5',
'No data', '4.1']}
# create a dataframe
df = pd.DataFrame(Data)
# converting each value of column to a string
df['Inflation Rate'] = pd.to_numeric(df['Inflation Rate'],
errors = 'coerce')
# show the dataframe
print(df)
# show the data types
print (df.dtypes)
输出:
注意:字符串数据类型显示为对象。