0
$\begingroup$

Lets say i have the following data like below:

  import pandas as pd
    import numpy as np
    df = pd.DataFrame({'Duration': ['1h 50m', '50m', '3h', '2h 30m', '5h', '60m']
                  })
    df

Generated Output:

enter image description here

Column Created in pandas data frame contains duration details in string format i.e (1h 50m,50m,3h etc). I need to know how to convert this column into an integer with the value displayed in minutes (110,50,180).

$\endgroup$
2
$\begingroup$

One liner:

df['Duration']=  df['Duration'].str.replace("h", '*60').str.replace(' ','+').str.replace('m','*1').apply(eval)

Basically, it converts your string into an equation in string format and evaluates it.

Hope it helps!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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