Reputation: 165
I am trying to split/extract a portion of the "Time" column so it will only show the hours and minutes e.g. 18:15 as opposed to 18:15:34.
I've seen alot of examples online that use the .str.split() function with highlighting the colon as the delimiter. But that would split the Time column into three columns: hours, minutes, seconds.
Input Dataframe:
df =
Index Time
0 18:15:21
1 19:15:21
2 20:15:21
3 21:15:21
4 22:15:21
Output Dataframe
df =
Index Time
0 18:15
1 19:15
2 20:15
3 21:15
4 22:15
Thanks :)
Upvotes: 1
Views: 2609
Reputation: 8816
You have fair choices here either replace
, extract
or split
with pandas.series.str
First, this is just case based solution..
Below solution with which does replace the last two number along with :
across Time
column.
>>> df['Time'] = df['Time'].str.replace(':\d{2}$', '')
>>> df
Time
0 18:15
1 19:15
2 20:15
3 21:15
4 22:15
Second approach with str.extract
with regex..
>>> df['Time'] = df['Time'].str.extract('(\d{2}:\d{2})')
>>> df
Time
0 18:15
1 19:15
2 20:15
3 21:15
4 22:15
\d{2} to hold initial two numbers
: next to match this immediately after first match
\d{2} again next two number followed by colon
$ asserts position at the end of a line
Upvotes: 1
Reputation: 249143
You can use a regex:
df.Time.str.replace(':\d\d$', '')
Or reverse-split:
df.Time.str.rsplit(':', 1).str[0]
Upvotes: 2
Reputation: 1293
You could use:
df['Time'].apply(lambda x : ':'.join(x.split(':')[0:2]))
Upvotes: 1