notsaggin
notsaggin

Reputation: 30

how to convert data with float, int and strings to just strings and float

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?

Upvotes: 1

Views: 47

Answers (1)

Matteo Buffagni
Matteo Buffagni

Reputation: 322

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')

Upvotes: 1

Related Questions