1

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?

1
  • you can convert string to float if they're actually number i.e. a string '3.2' can be converted, a string like 'hello' can't be converted. You may want to convert the string into its ASCII code and convert it with ` float()` but it's some kind of process to include Commented Jul 7, 2022 at 12:29

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.