Pandas – Convert String to Float in DataFrame

By using pandas.DataFrame.astype() and pandas.to_numeric() methods you can convert a column from string 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 of the quick examples of how to convert string to float in Pandas DataFrame. 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 entire DataFrame string 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 and execute some 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) results are as below. Here object means a String type.


Fee         object
Discount    object
dtype: object

2. Using DataFrame.astype(float) to Convert String to Float

Use df['Fee'] = df['Fee'].astype(float) to convert the values of "Fee" column from string to float type. astype(float) perform the conversion from string to float on DataFrame.


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

Yields below output.


Fee         float64
Discount     object
dtype: object

3. Convert String to Float Under the Entire DataFrame Using DataFrame.astype(float)

You can use the following syntax, df = df.astype(float) to convert all string columns to float type.


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

Yields below output.


Fee         float64
Discount    float64
dtype: object

4. Using pandas.to_numeric() to Convert DataFrame Column From String to Float

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

5. Numeric and non-numeric Values Using Convert String to Float

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)

In our case yields the same output as above.

6. Replace the ‘NaN’ Values with Zeros After Converting to Float

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

7.Replace Empty String (”) with np.nan Using DataFrame.replace()

In case if you wanted to replace empty string ('') with np.nan before converting string to float. For instance: df['Discount']=df.Discount.replace('',np.nan).astype(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. Also, you have learned to convert string to floats in DataFrame using replace empty string (”) with np.nan Using DataFrame.replace() 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

Pandas – Convert String to Float in DataFrame