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.
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
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
hours=pd.to_timedelta(df['Duration'])
df['Duration_hours']=hours.dt.components['hours']
this will help
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.
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
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
hours=pd.to_timedelta(df['Duration'])
df['Duration_hours']=hours.dt.components['hours']
this will help