There's probably a half-dozen tricky ways to do this in Python, here's a few:
- if you don't explicitly need to use pandas, you could try polars which doesn't use the
object
data type - if you want/need to use pandas, you can explicitly write out the dtypes on your statement that creates the dataframe (such as
pd.read_csv
. That can be tedious, but this is a good trick for not writing out every column type - (this one is probably the one I'd start with) given you already have a df, you can use
df.dtypes
to get the column types. Loop over that series and test "is this an object type?". If so, then do the conversion you wrote above. This means you'll only write the logic once, then for however many columns you have it will convert them all
After that, you're probably getting further into things like "read it in polars, then convert to a pandas df" if you have to use pandas, or figure out how to make your object columns that represent numbers actually be Int/float, but I'd start with one of the choices above first.
Best,
Randy