I am trying to pass a function that truncates a timestamp to a single column. It's performing the function but returning a list. I'm hoping to keep the data structure.
df = pd.DataFrame({
'Time' : ['8:03:001','8:17:004','8:20:003','8:28:002','8:35:004','8:40:006','8:42:002','8:45:004','8:50:009'],
'Place' : ['House 1','House 1','House 1','House 2','House 2','House 2','House 3','House 3','House 3'],
})
def truncate_time(col):
col = [x[:-2] for x in col]
return col
df1 = (truncate_time(df['Time']))
Intended Output:
Time Place
0 8:03:0 House 1
1 8:17:0 House 1
2 8:20:0 House 1
3 8:28:0 House 2
4 8:35:0 House 2
5 8:40:0 House 2
6 8:42:0 House 3
7 8:45:0 House 3
8 8:50:0 House 3
You can assign back:
df['Time'] = truncate_time(df['Time'])
print (df)
Time Place
0 8:03:0 House 1
1 8:17:0 House 1
2 8:20:0 House 1
3 8:28:0 House 2
4 8:35:0 House 2
5 8:40:0 House 2
6 8:42:0 House 3
7 8:45:0 House 3
8 8:50:0 House 3
But here is also possible use str
with indexing:
df['Time'] = df['Time'].str[:-2]
Or lambda function:
df['Time'] = df['Time'].apply(lambda col: col[:-2])
Or for function simplify solution with remove list comprehension with Series.apply
:
def truncate_time(col):
return col[:-2]
df['Time'] = df['Time'].apply(truncate_time)
And last solution with list comprehension:
df['Time'] = [x[:-2] for x in df['Time']]
EDIT: Performance with possible missing values - depends of number of values and also number of missing values:
#added one row with missing value
df = pd.DataFrame({
'Time' : ['8:03:001','8:17:004','8:20:003','8:28:002','8:35:004','8:40:006','8:42:002','8:45:004','8:50:009',np.nan],
'Place' : ['House 1','House 1','House 1','House 2','House 2','House 2','House 3','House 3','House 3','House 3'],
})
def truncate_time(col):
return col[:-2] if col == col else col
#[1000000 rows x 2 columns]
df = pd.concat([df] * 100000, ignore_index=True)
In [104]: %timeit df['Time1'] = df['Time'].str[:-2]
460 ms ± 20.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [105]: %timeit df['Time2'] = [x[:-2] if x == x else x for x in df['Time']]
445 ms ± 9.72 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [106]: %timeit df['Time3'] = df['Time'].apply(lambda col: col[:-2] if col == col else col)
428 ms ± 18.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [107]: %timeit df['Time4'] = df['Time'].apply(truncate_time)
416 ms ± 8.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.