How to split 'Hour' and 'Minute' from my dataset

Question:

I have been trying to split ‘h’ & ‘m’ from my column data and extract it into another column.

Here is my data:

   Duration
   2h 50m
      25m
   19h

And this is my code:

duration = list(datasets['Duration'])

for i in range(len(duration)):
    if len(duration[i].split()) != 2:
    if 'h' in duration[i]:
        duration[i] = duration[i].strip() + '0m'
    else:
        duration[i] = '0h' + duration[i]
        
   duration_hours = []
   duration_mins = []

   for i in range(len(duration)):
      duration_hours.append(int(duration[i].split(sep = 'h')[0]))
      duration_mins.append(int(duration[i].split(sep = 'm')[0].split()[-1]))

I am trying to split hours and minutes and trying to extract it into hours and minutes column.

Asked By: Raptor795

||

Answers:

Try with pd.to_timedelta and with accesor dt.components:

s=pd.to_timedelta(df['Duration'])
df['hours']=s.dt.components['hours']
df['minutes']=s.dt.components['minutes']

print(df)
  Duration  hours  minutes
0   2h 50m     2       50
1      25m     0       25
2      19h    19        0
Answered By: MrNobody33
if 'h' in duration[i]:
    duration[i] = duration[i].strip() + ' 0m' #----> just add the space here " 0m"
else:
    duration[i] = '0h ' + duration[i] #------> add the space here "0h " as well
Answered By: SOMEET SINGH
hours=pd.to_timedelta(df['Duration'])
df['Duration_hours']=hours.dt.components['hours']

this will help

Answered By: Jayesh Vadje
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.