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

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

如何在 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。

示例:在此示例中,我们将“通货膨胀率”列的每个值转换为浮点数。

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()函数。

该函数用于将参数转换为数值类型。

示例 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)

输出:

使用错误处理浮动的数据帧字符串

注意:字符串数据类型显示为对象。