Issue
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.
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.