如何在Pandas数据框架中把浮点数转换成字符串
在这篇文章中,我们将看到在Pandas Dataframe中把浮点数转换成字符串的不同方法?Pandas Dataframe提供了改变列值的数据类型的自由。我们可以将它们从整数变为浮点类型,整数变为字符串,字符串变为整数,浮点变为字符串,等等。
有三种方法可以将Float转换为String。
方法1:使用DataFrame.astype()
语法 :
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
这是用来将一个pandas对象投到一个指定的dtype。这个函数也提供了将任何合适的现有列转换为分类类型的能力。
例子1:将列从浮点数转换为字符串。
# 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)
输出:
现在,我们将 “Marks “列的数据类型从 “float64 “改为 “object”。
# 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:将多于的一列从浮点数转换为字符串。
# 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)
输出:
现在,我们将 “准确度 “和 “年龄 “两列的数据类型从 “float64 “改为 “object”。
# 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:使用系列.apply()。
语法 :
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)
这个方法允许用户传递一个函数,并将其应用于Pandas系列的每一个值。
例子:将数据框架的列从浮点转换成字符串。
# 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 “改为 “对象”。
# 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)
这种方法用于映射两个系列的数值,这两个系列有一列相同。
例子:将数据框架的列从浮点转换成字符串。
# 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)
输出:
现在,我们将列 “年龄 “的数据类型从 “float64 “改为 “object”。
# 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
输出: