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

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

如何在 Pandas DataFrame 中将浮点数转换为字符串?

在这篇文章中,我们将看到在 Pandas Dataframe 中将浮点数转换为字符串的不同方法? Pandas Dataframe 提供了更改列值数据类型的自由。我们可以将它们从 Integers 更改为 Float 类型,Integer 更改为 String,String 更改为 Integer,Float 更改为 String 等。

将浮点数转换为字符串的三种方法:

方法 1:使用DataFrame.astype()

语法

DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)

这用于将 pandas 对象转换为指定的 dtype。此函数还提供将任何合适的现有列转换为分类类型的功能。

示例 1:列从 float 转换为字符串。

Python3
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey', 10, 45.25], ['Carson', 15, 54.85],
        ['juli', 14, 87.21], ['Ricky', 20, 45.23],
        ['Gregory', 21, 77.25], ['Jessie', 16, 95.21]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Marks'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data type 
# of 'Marks' column
print (df.dtypes)


Python3
# Now we will convert it from 
# 'float' to 'String' type. 
df['Marks'] = df['Marks'].astype(str)
  
print()
  
# lets find out the data
# type after changing
print(df.dtypes)
  
# print dataframe. 
df


Python3
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8], 
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Marks', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data type 
# of 'Age' and 'Accuracy' columns
print (df.dtypes)


Python3
# Now Pass a dictionary to 
# astype() function which contains 
# two columns and hence convert them
# from float to string type
df = df.astype({"Age": 'str', "Accuracy": 'str'})
print()
  
# lets find out the data 
# type after changing
print(df.dtypes)
  
# print dataframe. 
df


Python3
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8],
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data 
# type of 'Percentage' column
print (df.dtypes)


Python3
# Now we will convert it from 
# 'float' to 'string' type. 
df['Percentage'] = df['Percentage'].apply(str) 
print()
  
# lets find out the data
# type after changing
print(df.dtypes)
  
# print dataframe. 
df


Python3
# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8], 
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data
# type of 'Age' column
print (df.dtypes)


Python3
# Now we will convert it from 'float' to 'string' type. 
# using DataFrame.map(str) function
df['Age'] = df['Age'].map(str)  
print()
  
# lets find out the data type after changing
print(df.dtypes)
  
# print dataframe. 
df


输出:

现在,我们将“ Marks”列的数据类型从“float64”更改为“object”。

Python3

# Now we will convert it from 
# 'float' to 'String' type. 
df['Marks'] = df['Marks'].astype(str)
  
print()
  
# lets find out the data
# type after changing
print(df.dtypes)
  
# print dataframe. 
df 

输出:

示例 2:列从 float 转换为字符串。

Python3

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8], 
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Marks', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data type 
# of 'Age' and 'Accuracy' columns
print (df.dtypes)

输出:

现在,我们将列“ Accuracy ”和“ Age ”的数据类型从“float64”更改为“object”。

Python3

# Now Pass a dictionary to 
# astype() function which contains 
# two columns and hence convert them
# from float to string type
df = df.astype({"Age": 'str', "Accuracy": 'str'})
print()
  
# lets find out the data 
# type after changing
print(df.dtypes)
  
# print dataframe. 
df 

输出:

方法 2:使用Series.apply()

语法

DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)

此方法允许用户传递一个函数并将其应用于 Pandas 系列的每个值。

示例:将 Dataframe 的列从 float 转换为字符串。

Python3

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8],
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data 
# type of 'Percentage' column
print (df.dtypes)

输出:

现在,我们将列“百分比”的数据类型从“float64”更改为“对象”。

Python3

# Now we will convert it from 
# 'float' to 'string' type. 
df['Percentage'] = df['Percentage'].apply(str) 
print()
  
# lets find out the data
# type after changing
print(df.dtypes)
  
# print dataframe. 
df

输出:

方法 3 :使用Series.map()

句法:

Series.map(arg, na_action=None)

此方法用于映射来自具有相同列的两个系列的值。

示例:将数据框的列从 float 转换为字符串。

Python3

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8], 
        ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],
        ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f']) 
  
# lets find out the data
# type of 'Age' column
print (df.dtypes)

输出:

现在,我们将“ Age ”列的数据类型从“float64”更改为“object”。

Python3

# Now we will convert it from 'float' to 'string' type. 
# using DataFrame.map(str) function
df['Age'] = df['Age'].map(str)  
print()
  
# lets find out the data type after changing
print(df.dtypes)
  
# print dataframe. 
df 

输出: