0

I try to convert a datafram's column having "object" as datatype into float.

I use the following code:

cam_dev_index_num = cam_dev_index['Access to electricity (% of population)'].astype(int(float()))

But I have this error: "Cannot interpret '0' as a data type"

Could someone explain me what I'm doing wrong please?

Thank you in advance!

3

1 Answer 1

1

Try this:

cam_dev_index_num = cam_dev_index['Access to electricity (% of population)'].astype(int).astype(float)

Or the other way around:

.astype(float).astype(int)

Perhaps even only one of the two is needed, just:

.astype(float)

Explanation: astype does not take a function as input, but a type (such as int).

2
  • Thanks Paul; indeed, .astype(float) was sufficient!
    – NewbiSnaky
    May 19, 2022 at 14:19
  • If this answered your question, please accept this answer. This way the community knows this problem was solved.
    – Paul
    May 25, 2022 at 9:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.