How to Convert Floats to Strings in Pandas DataFrame

In this short guide, you’ll see 3 approaches to convert floats to strings in Pandas DataFrame for:

(1) An individual DataFrame column using astype(str):

df['DataFrame Column'] = df['DataFrame Column'].astype(str)

(2) An individual DataFrame column using apply(str):

df['DataFrame Column'] = df['DataFrame Column'].apply(str)

(3) An entire DataFrame using:

df = df.astype(str)

Next, you’ll see how to apply each of the above approaches using simple examples.

3 Approaches to Convert Floats to Strings in Pandas DataFrame

(1) Convert floats to strings for an individual DataFrame column using astype(str)

Here is an example of a DataFrame with a single column (called ‘numeric_values‘) that contains only floats:

import pandas as pd
   
data = {'numeric_values': [22.0, 9.0, 557.0, 15.995, 225.12]}
df = pd.DataFrame(data,columns=['numeric_values'])

print(df)
print(df.dtypes)

Run the code, and you’ll see that the data type of the ‘numeric_values’ column is float:

How to Convert Floats to Strings in Pandas DataFrame

You can then convert the floats to strings using astype(str):

df['DataFrame Column'] = df['DataFrame Column'].astype(str)

So the complete Python code to perform the conversion is:

import pandas as pd
   
data = {'numeric_values': [22.0, 9.0, 557.0, 15.995, 225.12]}
df = pd.DataFrame(data,columns=['numeric_values'])

df['numeric_values'] = df['numeric_values'].astype(str)

print(df)
print(df.dtypes)

As you can see, the new data type of the ‘numeric_values’ column is ‘object‘ which represents strings:

Convert Floats to Strings in Pandas DataFrame

(2) Convert floats to strings for an individual DataFrame column using apply(str)

Optionally, you can convert the floats to strings using apply(str):

df['DataFrame Column'] = df['DataFrame Column'].apply(str)

Here is the complete code to conduct the conversion to strings:

import pandas as pd
   
data = {'numeric_values': [22.0, 9.0, 557.0, 15.995, 225.12]}
df = pd.DataFrame(data,columns=['numeric_values'])

df['numeric_values'] = df['numeric_values'].apply(str)

print(df)
print(df.dtypes)

As before, the new data type of the ‘numeric_values’ column is object:

Object

(3) Convert an entire DataFrame to strings

In the final case, let’s create a DataFrame with 3 columns, where the data type of all those columns is float:

import pandas as pd
   
data = {'numeric_values_1': [22.0, 9.0, 557.0, 15.995, 225.12],
        'numeric_values_2': [79.0, 5.0, 428.0, 19.883, 514.03],
        'numeric_values_3': [48.0, 7.0, 325.0, 12.772, 802.09]
       }
df = pd.DataFrame(data,columns=['numeric_values_1','numeric_values_2','numeric_values_3'])

print(df)
print(df.dtypes)

As you can observe, the data type of all the columns in the DataFrame is indeed float:

Example of a dataset

To convert the entire DataFrame from floats to strings, you may use:

df = df.astype(str)

So the complete code is as follows:

import pandas as pd
   
data = {'numeric_values_1': [22.0, 9.0, 557.0, 15.995, 225.12],
        'numeric_values_2': [79.0, 5.0, 428.0, 19.883, 514.03],
        'numeric_values_3': [48.0, 7.0, 325.0, 12.772, 802.09]
       }
df = pd.DataFrame(data,columns=['numeric_values_1','numeric_values_2','numeric_values_3'])

df = df.astype(str)

print(df)
print(df.dtypes)

You’ll now get the newly data type of object across all the columns in the DataFrame:

How to Convert Floats to Strings in Pandas DataFrame

Additional Resources

You can visit the Pandas Documentation to learn more about astype.

Alternatively, you may review the following guides for other types of conversions: