dataframe
pandas
python
data-conversion
data-types

Convert dataframe columns of object type to float

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In pandas, columns with dtype object often contain numbers stored as strings. That is common after reading CSV files, scraping data, or merging inconsistent sources. Converting those columns to float is usually simple, but only if you first decide how to handle bad values such as empty strings, commas, or mixed text.

Start with One Clean Column

If a column contains numeric-looking strings, the most direct conversion is:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "price": ["10.5", "20.0", "30.25"]
5})
6
7df["price"] = df["price"].astype(float)
8print(df.dtypes)

That works only when every value is already convertible. If any row contains "N/A" or "1,200", astype(float) raises an exception.

Use pd.to_numeric for Real Data

For messy data, pd.to_numeric is safer because it lets you choose what to do with invalid values.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "price": ["10.5", "bad", "30.25", None]
5})
6
7df["price"] = pd.to_numeric(df["price"], errors="coerce")
8print(df)
9print(df.dtypes)

With errors="coerce", invalid values become NaN instead of crashing the conversion. That is often the right default for analysis pipelines because it makes the problem visible while still letting the column become numeric.

Clean Formatting Before Conversion

Real object columns often fail conversion for formatting reasons rather than truly non-numeric content. Common examples include:

  • commas in thousands separators
  • percent signs
  • leading or trailing whitespace
  • currency symbols

Clean the text first, then convert:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "amount": ["1,200.50", " 300.00 ", "$45.90"]
5})
6
7df["amount"] = (
8    df["amount"]
9    .str.replace(",", "", regex=False)
10    .str.replace("$", "", regex=False)
11    .str.strip()
12)
13
14df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
15print(df)

The order matters. Cleaning first usually keeps more valid rows.

Convert Several Object Columns at Once

If multiple columns should be numeric, select them explicitly instead of converting every object column blindly. Some object columns are really categorical text and should stay that way.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "a": ["1.0", "2.5", "3.2"],
5    "b": ["4", "5", "bad"],
6    "name": ["x", "y", "z"],
7})
8
9cols = ["a", "b"]
10df[cols] = df[cols].apply(pd.to_numeric, errors="coerce")
11
12print(df)
13print(df.dtypes)

This keeps the conversion targeted and easier to review.

Know the Difference Between Conversion and Inference

Pandas can sometimes infer numeric dtype during file import, but inference is not guaranteed. If a column contains one bad string among many numeric rows, pandas may load the whole column as object.

That is why conversion is often a cleanup step after reading the file:

python
df = pd.read_csv("data.csv")
df["price"] = pd.to_numeric(df["price"], errors="coerce")

If bad values are unexpected, you may prefer errors="raise" while debugging so the pipeline fails loudly.

Float Is Not Always the Best Target

People often say "convert to float" when they really mean "convert to numeric." If a column contains integers only, Int64 or int64 may be a better type. If precision matters, such as currency calculations, float may not be ideal at all.

Still, float is a common target because it can represent NaN, which makes mixed-quality data easier to handle during cleanup.

Common Pitfalls

  • Using astype(float) on dirty data and being surprised when one bad value crashes the whole conversion.
  • Converting every object column blindly, including text columns that should remain strings.
  • Forgetting to strip commas, currency symbols, or whitespace before conversion.
  • Assuming object means text only; it is really a catch-all dtype.
  • Treating float conversion as semantically correct for every numeric field, even when integer or decimal precision matters more.

Summary

  • 'object columns often contain numeric strings that need explicit conversion.'
  • Use astype(float) only when the data is already clean.
  • Use pd.to_numeric(..., errors="coerce") for safer real-world conversions.
  • Clean formatting artifacts such as commas and currency symbols before conversion.
  • Convert only the columns that are supposed to become numeric, not every object column automatically.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design