In this post, we’ll see different ways to Convert Floats to Strings in Pandas Dataframe? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, Float to String, etc.
There are three methods to convert Float to String:
Method 1: Using DataFrame.astype().
Syntax :
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
This is used to cast a pandas object to a specified dtype. This function also provides the capability to convert any suitable existing column to categorical type.
Example 1: Converting one column from float to string.
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) |
Output:

Now, we change the data type of column ‘Marks’ from ‘float64’ to ‘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 |
Output:

Example 2: Converting more than one column from float to string.
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) |
Output:

Now, we change the data type of columns ‘Accuracy‘ and ‘Age‘ from ‘float64’ to ‘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 |
Output:

Method 2: Using Series.apply().
Syntax :
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)
This method allows the users to pass a function and apply it on every single value of the Pandas series.
Example: Converting column of a Dataframe from float to string.
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) |
Output:

Now, we change the data type of column ‘Percentage‘ from ‘float64′ to ”object’.
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 |
Output:

Method 3: Using Series.map().
Syntax:
Series.map(arg, na_action=None)
This method is used to map values from two series having one column same.
Example: Converting column of a dataframe from float to string.
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) |
Output:

Now, we change the data type of column ‘Age‘ from ‘float64′ to ”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 |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- How to Convert Strings to Floats in Pandas DataFrame?
- How to Convert Integers to Floats in Pandas DataFrame?
- Convert Floats to Integers in a Pandas DataFrame
- Python | Ways to convert array of strings to array of floats
- Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- How to Convert Integers to Strings in Pandas DataFrame?
- Fastest way to Convert Integers to Strings in Pandas DataFrame
- Convert a series of date strings to a time series in Pandas Dataframe
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Convert the column type from string to datetime format in Pandas dataframe
- Python | Convert list of nested dictionary into Pandas dataframe
- How to convert pandas DataFrame into JSON in Python?
- How to Convert Integer to Datetime in Pandas DataFrame?
- How to Convert Dataframe column into an index in Python-Pandas?
- How to Convert Index to Column in Pandas Dataframe?
- How to Convert Pandas DataFrame into a List?
- How to Convert String to Integer in Pandas DataFrame?
- How to convert Dictionary to Pandas Dataframe?
- How to convert pandas DataFrame into SQL in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

