Convert Floats to Strings in Pandas DataFrame

Here are 3 approaches to convert floats to strings in Pandas DataFrame for:

(1) A single DataFrame column using astype(str):

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

(2) A single DataFrame column using apply(str):

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

(3) An entire DataFrame using:

df = df.astype(str)

3 Approaches to Convert Floats to Strings in Pandas DataFrame

(1) 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)

print(df)
print(df.dtypes)

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

   numeric_values
0          22.000
1           9.000
2         557.000
3          15.995
4         225.120
numeric_values    float64
dtype: object

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)

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

print(df)
print(df.dtypes)

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

  numeric_values
0           22.0
1            9.0
2          557.0
3         15.995
4         225.12
numeric_values    object
dtype: object

(2) 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)

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

print(df)
print(df.dtypes)

As before, the new data type is object:

  numeric_values
0           22.0
1            9.0
2          557.0
3         15.995
4         225.12
numeric_values    object
dtype: object

(3) Convert an entire DataFrame to strings

In the final case, create a DataFrame with 3 columns, where the data type for each column 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)

print(df)
print(df.dtypes)

As can be observed, the data type for all the columns in the DataFrame is indeed float:

   numeric_values_1  numeric_values_2  numeric_values_3
0            22.000            79.000            48.000
1             9.000             5.000             7.000
2           557.000           428.000           325.000
3            15.995            19.883            12.772
4           225.120           514.030           802.090
numeric_values_1    float64
numeric_values_2    float64
numeric_values_3    float64
dtype: object

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)
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:

  numeric_values_1 numeric_values_2 numeric_values_3
0             22.0             79.0             48.0
1              9.0              5.0              7.0
2            557.0            428.0            325.0
3           15.995           19.883           12.772
4           225.12           514.03           802.09
numeric_values_1    object
numeric_values_2    object
numeric_values_3    object
dtype: object

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:

Leave a Comment