Coding Ref

How to convert string to float in Pandas

How to convert string to float in Pandas

To convert a string to a float in Pandas, you can use the to_numeric method. This method takes a Series object containing the string values, and returns a new Series object with the converted values.

For example, consider the following Series object containing string values:

main.py
import pandas as pd

# Create a Series object containing string values
s = pd.Series(['1.5', '2.3', '3.14', '4.0'])

This Series has four elements, each of which is a string representing a floating-point number.

To convert these values to floats, you could do the following:

main.py
import pandas as pd

# Create a Series object containing string values
s = pd.Series(['1.5', '2.3', '3.14', '4.0'])

# Convert the values to floats
s = pd.to_numeric(s)

# Print the resulting Series
print(s)

In the code above, the to_numeric method is applied to the Series object containing the string values.

This converts the values to floats and returns a new Series object with the converted values. In this case, the resulting Series has the values 1.5, 2.3, 3.14, and 4.0.

If the string values in the Series cannot be converted to floats, the to_numeric method will raise a ValueError exception.

To avoid this, you can specify the errors argument and set it to 'coerce', which will convert any values that cannot be converted to NaN (not a number) values instead of raising an exception.

For example, the following code converts the string values to floats, and replaces any values that cannot be converted with NaN values:

main.py
import pandas as pd

# Create a Series object containing string values
s = pd.Series(['1.5', '2.3', '3.14', 'four'])

# Convert the values to floats, replacing any values that cannot be converted with NaN
s = pd.to_numeric(s, errors='coerce')

# Print the resulting Series
print(s)

In the code above, the to_numeric method is applied to the Series object containing the string values.

The errors argument is set to 'coerce', which tells the to_numeric method to replace any values that cannot be converted with NaN values.

In this case, the resulting Series has the values 1.5, 2.3, 3.14, and NaN.

You'll also like

Related tutorials curated for you

    How to change the order of columns in Pandas

    What is idxmax() in Pandas?

    fillna() in Pandas

    How to add an empty column to a Pandas DataFrame

    What is isna() in Pandas?

    What is categorical data in Pandas?

    How to get the number of columns in a Pandas DataFrame

    How to use ffill() in Pandas

    How to use Timedelta in Pandas

    How to use astype() in Pandas

    How to split a Pandas DataFrame by a column value

    How to use nunique() in Pandas