search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Converting column type to float in Pandas DataFrame

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Data Manipulation Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Solution

To convert the column type to float in Pandas DataFrame:

We recommend using to_numeric() since this method is more flexible.

Example - converting data type of a single column

Consider the following DataFrame:

df = pd.DataFrame({"A":["3","4"],"B":["5","6"]})
df
A B
0 3 5
1 4 6

Currently, the column types are as follows:

A object
B object
dtype: object

Using astype method

To convert column A into type float, use the Series' astype() method:

df["A"] = df["A"].astype("float")
A float64
B object
dtype: object

Using as_numeric method

To convert column A into type float32, use the Pandas' to_numeric(~) method:

df["A"] = pd.to_numeric(df["A"], downcast="float")
A float32
B object
dtype: object

Case when conversion is not possible

Consider the following DataFrame:

df = pd.DataFrame({"A":["3#","4"]})
df
A
0 3#
1 4

Here, the value "3#" cannot be converted into a numeric type. By default, the to_numeric(~) type will throw an error in such cases:

df["A"] = pd.to_numeric(df["A"])
ValueError: Unable to parse string "3#" at position 0

We can map values that cannot be converted into NaN instead:

df["A"] = pd.to_numeric(df["A"], errors='coerce')
A float64
B object
dtype: object

Note that Pandas will only allow columns containing NaN to be of type float.

Example - converting data type of multiple columns to float

To convert the data type of multiple columns to float, use Pandas' apply(~) method with to_numeric(~).

Case when conversion is possible

Consider the following DataFrame:

df = pd.DataFrame({"A":["3","4"],"B":["5","6"]})
df
A B
0 3 5
1 4 6

Currently, the column types are as follows:

A object
B object
dtype: object

To convert the type of all the columns, use the DataFrame's apply(~) method:

df = df.apply(pd.to_numeric)
A int64
B int64
dtype: object

Here, we are iteratively applying Pandas' to_numeric(~) method to each column of the DataFrame. The to_numeric(~) method takes as argument a single column (Series) and converts its type to numeric (e.g. int or float).

Case when conversion is not possible

Consider the following DataFrame:

df = pd.DataFrame({"A":["3","4"],"B":["5#","6"]})
df
A B
0 3 5#
1 4 6

Here, column B cannot be converted into numeric type since 5# is not a valid number. Applying the to_numeric(~) method without arguments will result in an error:

df = df.apply(pd.to_numeric)
ValueError: Unable to parse string "5#" at position 0

Ignoring unsuccessful columns

Instead of throwing an error, we can supply the following keyword argument to to_numeric() in order to ignore columns where the conversion is not possible:

df = df.apply(pd.to_numeric, errors='ignore', downcast='float')
A float32
B object
dtype: object

Replace with NaN for unsuccessful values

To fill values that cannot be successfully converted into the specified data type with NaN:

df = df.apply(pd.to_numeric, errors='coerce', downcast='float')
df
A B
0 3 NaN
1 4 6.0

Here, the value "5#" could not be converted into a numeric type and therefore we end up with a NaN instead. The converted data types are as follows:

A float32
B float32
dtype: object
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...