Home > OS >  how to convert data with float, int and strings to just strings and float
how to convert data with float, int and strings to just strings and float

Time:07-09

I have a csv with data in different formats. there are float, int and strings.

How can I modify just the int into float?

I have not tried much, just with this:

df=df.astype(dtype=np.float64, copy=True, errors='raise')

but cant manage to make it work, because there are also strings

How can I solve this?

CodePudding user response:

The columns of a pandas DataFrame (or a Series) are homogeneously of type. You can inspect this with dtype (or DataFrame.dtypes)

You can select_dtypes first and and finally convert to float using df.astype

tmp = df.select_dtypes(include=['Int64'])
df[tmp.columns]= tmp.astype('float64')
  • Related