1

In my df I have some values set up as dtype(str)

      x
  27:47
  13:45
  10:45

And I would like to convert them to float, ending up with:

      x
  27.47
  13.45
  10.45

How do I do this?

4
  • 1
    but 27 mins, 47 seconds is not 27.47 minutes? Commented Oct 25, 2020 at 2:24
  • it can be '.', no problem Commented Oct 25, 2020 at 2:25
  • I mean 1 minute = 60 seconds, so 47 seconds is about .76 minute, not .47 minute. Commented Oct 25, 2020 at 2:26
  • the name does not matter, really, just the conversion Commented Oct 25, 2020 at 2:28

2 Answers 2

1

In your case, you can do:

df['x'] = df['x'].str.replace(':','.').astype(float)

Output:

       x
0  27.47
1  13.45
2  10.45
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

df['x'] = df['x'].replace(to_replace=":",value=".").astype(float)

5 Comments

.str is needed.
Nope. Beacuse this is Pandas .replace() method
well, the solution below worked and the only difference is that one....
yes...I made sure to pass df.x.astype(str)...but somehow it only works with .str before replace
For earlier me it worked without .str, still can't figure out what's the cause

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.