I have the following dataframe with 4 columns:
IP Time URL Staus
0 10.128.2.1 [29/Nov/2017:06:58:55 GET /login.php HTTP/1.1 200
1 10.128.2.1 [29/Nov/2017:06:59:02 POST /process.php HTTP/1.1 302
2 10.128.2.1 [29/Nov/2017:06:59:03 GET /home.php HTTP/1.1 200
3 10.131.2.1 [29/Nov/2017:06:59:04 GET /js/vendor/moment.min.js HTTP/1.1 200
4 10.130.2.1 [29/Nov/2017:06:59:06 GET /bootstrap-3.3.7/js/bootstrap.js HTTP/1.1 200
5 10.130.2.1 [29/Nov/2017:06:59:19 GET /profile.php?user=bala HTTP/1.1 200
I need to split the Time column into two new columns titled 'date' and 'time'. I need to split the current value under the Time column by the first occurrence of ':'.
I have tried the split function for the first instance of ':' as follows:
df['date','time']=df.Time.str.split(":", 1)
But this is what i end up getting:
IP Time URL Staus (date, time)
0 10.128.2.1 [29/Nov/2017:06:58:55 GET /login.php HTTP/1.1 200 [[29/Nov/2017, 06:58:55]
1 10.128.2.1 [29/Nov/2017:06:59:02 POST /process.php HTTP/1.1 302 [[29/Nov/2017, 06:59:02]
2 10.128.2.1 [29/Nov/2017:06:59:03 GET /home.php HTTP/1.1 200 [[29/Nov/2017, 06:59:03]
3 10.131.2.1 [29/Nov/2017:06:59:04 GET /js/vendor/moment.min.js HTTP/1.1 200 [[29/Nov/2017, 06:59:04]
How do I properly split into two columns? What am I doing wrong? Help :(
Add parameter expand=True
for DataFrame
and then add []
for new columns:
df[['date','time']] = df.Time.str.split(":", 1, expand=True)
print (df)
IP Time URL Staus \
0 10.128.2.1 [29/Nov/2017:06:58:55 GET/login.php HTTP/1.1 200
1 10.128.2.1 [29/Nov/2017:06:59:02 POST/process.php HTTP/1.1 302
date time
0 [29/Nov/2017 06:58:55
1 [29/Nov/2017 06:59:02
Or also add Series.str.strip
for remove trailing []
:
df[['date','time']] = df.Time.str.strip('[]').str.split(":", 1, expand=True)
print (df)
IP Time URL Staus \
0 10.128.2.1 [29/Nov/2017:06:58:55 GET/login.php HTTP/1.1 200
1 10.128.2.1 [29/Nov/2017:06:59:02 POST/process.php HTTP/1.1 302
date time
0 29/Nov/2017 06:58:55
1 29/Nov/2017 06:59:02
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.