Pandas Convert Column to Float in DataFrame

By using pandas DataFrame.astype() and pandas.to_numeric() methods you can convert a column from string/int type to float. In this article, I will explain how to convert one or multiple string columns to float type using examples.

1. Quick Examples of Convert String to Float in DataFrame

If you are in a hurry, below are some quick examples of how to convert string to float. You can apply these to convert from any type in Pandas


# Below are quick example
# convert "Fee" from string to float
df['Fee'] = df['Fee'].astype(float)
print(df.dtypes)

# Convert multiple columns
df = df.astype({'Fee':'float','Discount':'float'})

# Convert all columns to floats
df = df.astype(float)
print(df.dtypes)

# Convert numeric function string to float
df['Discount'] = pd.to_numeric(df['Discount'])
print(df.dtypes)

# Convert DataFrame column from string to float
df["Discount"] = pd.to_numeric(df["Discount"], downcast="float")
print(df.dtypes)

# Convert each value of the column to a string
df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce')
print(df.dtypes)

# Using df.replace() to replace nan values 0 before convertion
df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce')
df = df.replace(np.nan, 0, regex=True)
print(df)
print(df.dtypes)

# Replace empty string ('') with np.nan before convertion
df['Discount']=df.Discount.replace('',np.nan).astype(float)
print(df)
print(df.dtypes)

Now, let’s create a DataFrame with a few rows and columns, execute the above examples and validate the results. Our DataFrame contains column names Fee and Discount.


import pandas as pd
import numpy as np
technologies= ({
         'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
         'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
             })
df = pd.DataFrame(technologies)
print(df.dtypes)

You can identify the data type of each column by using dtypes. For Instance, print(df.dtypes) outputs as below. Here object means a String type.


Fee         object
Discount    object
dtype: object

2. pandas Convert String to Float

Use pandas DataFrame.astype() function to convert column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to 54-bit signed float, you can use numpy.float64,numpy.float_float, float64 as param. To cast to 32-bit signed float, use numpy.float32 or float32.

The Below example converts Fee column from string dtype to float64.


# Convert "Fee" from string to float
df = df.astype({'Fee':'float'})
print(df.dtypes)

Yields below output.


Fee         float64
Discount     object
dtype: object

You can also use Series.astype() to convert a specific column. since each column on DataFrame is pandas Series, I will get the column from DataFrame as Series and use astype() function. In the below example df.Fee or df['Fee'] returns Series object.


# Convert "Fee" from string to float
df['Fee'] = df['Fee'].astype(float)
print(df.dtypes)

Yields same output as above.

3. Convert Multiple Columns to Float

You can also convert multiple columns to float by sending dict of column name -> data type to astype() method. The below example converts column Fee  and Discount to float dtype.


# Convert multiple columns
df = df.astype({'Fee':'float','Discount':'float'})

4. Convert All Columns to Float Type

By default astype() function converts all columns to the same type. The below example converts all DataFrame columns to float type. If you have any column with alpha-numeric values, you will get an error.


# Convert entire DataFrame string to float
df = df.astype(float)
print(df.dtypes)

Yields below output.


Fee         float64
Discount    float64
dtype: object

5. Using pandas.to_numeric()

Alternatively, you can convert all string columns to float type using pandas.to_numeric(). For example use df['Discount'] = pd.to_numeric(df['Discount']) function to convert ‘Discount’ column to float.


# Convert numeric function string to float
df['Discount'] = pd.to_numeric(df['Discount'])
print(df.dtypes)

# Convert DataFrame column from string to float
df["Discount"] = pd.to_numeric(df["Discount"], downcast="float")
print(df.dtypes)

Yields below output.


Fee          object
Discount    float64
dtype: object

6. Handling Non-numeric Values

When you have some cells with character values on a column you wanted to convert to float, it returns an error. To ignore the error and convert the char values to NaN use errors='coerce' attribute.


# Convert each value of the column to a string
df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce')
print(df.dtypes)

This yields the same output as above.

7. Replace the ‘NaN’ Values with Zeros

Use df=df.replace(np.nan,0,regex=True) function to replace the ‘NaN’ values with ‘0’ values.


# Using df.replace() to replace nan values 0
df['Discount'] = pd.to_numeric(df['Discount'], errors='coerce')
df = df.replace(np.nan, 0, regex=True)
print(df)
print(df.dtypes)

Yields below output.


        Fee  Discount
0  22000.30    1000.1
1  25000.40       0.0
2  23000.20    1000.5
3  24000.50       0.0
4  26000.10    2500.2
Fee          object
Discount    float64
dtype: object

8. Replace Empty String before Convert

If you have empty values in a string, convert empty string ('') with np.nan before converting it to float.


import pandas as pd
import numpy as np
technologies= ({
         'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10','21000'],
         'Discount':['1000.10',np.nan,"",np.nan,'2500.20',""]
             })
df = pd.DataFrame(technologies)
# Replace empty string ('') with np.nan
df['Discount']=df.Discount.replace('',np.nan).astype(float)
print(df)
print(df.dtypes)

Yields below output.


        Fee  Discount
0  22000.30    1000.1
1  25000.40       NaN
2  23000.20       NaN
3  24000.50       NaN
4  26000.10    2500.2
5     21000       NaN
Fee          object
Discount    float64
dtype: object

Conclusion

In this article, you have learned how to convert single, multiple, and all columns from string type to float in Pandas DataFrame using DataFrame.astype(float) and pandas.to_numeric() function.

Happy Learning !!

You May Also Like

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

This Post Has 2 Comments

  1. Marco

    Thank you very much for your help !

    1. NNK

      Marco, Glad it was helpful.

You are currently viewing Pandas Convert Column to Float in DataFrame