我有一个这样的数据框:
Id row Date BuyTime SellPrice App
1 1 2017-10-30 94520 0 9:00:00
1 2 2017-10-30 94538 0 9:00:00
1 3 2017-10-30 94609 0 9:00:00
1 4 2017-10-30 94615 0 9:00:00
1 5 2017-10-30 94617 0 9:00:00
1 1 2017-09-20 99100 99159 9:00:10
1 2 2017-09-20 99102 99058 9:00:11
1 3 2017-09-20 99103 99057 9:00:12
1 4 2017-09-20 99104 99056 9:00:10
1 5 2017-09-20 99105 99055 9:00:10
1 1 2017-09-20 99100 99190 9:01:10
1 2 2017-09-20 98099 99091 9:01:10
1 3 2017-09-20 98098 99092 9:01:10
1 4 2017-09-20 98097 99093 9:01:10
1 5 2017-09-20 98096 99094 9:01:10
2 1 2010-11-01 99890 100000 10:00:02
2 2 2010-11-01 99899 100000 10:00:02
2 3 2010-11-01 99901 99899 9:00:02
2 4 2010-11-01 99920 99850 10:00:02
2 5 2010-11-01 99933 99848 10:00:23
我想将BuyTime格式更改为时间格式(%H:%M:%S),然后创建一个新列并合并所有行的Date和BuyTime,例如:
Id row Date BuyTime SellPrice App TimeStamp
1 1 2017-10-30 09:45:20 0 9:00:00 2017-10-30 09:45:20
答案 0 :(得分:1)
使用str.zfill
与str[]
一起选择字符串的位置然后to_timedelta
:
s = df['BuyTime'].astype(str).str.zfill(6)
df['BuyTime'] = s.str[:2] + ':' + s.str[2:4] + ':' + s.str[4:]
df['TimeStamp'] = df['Date'] + pd.to_timedelta(df['BuyTime'])
#if not datetime column convert it
#df['TimeStamp'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['BuyTime'])
print (df)
Id row Date BuyTime SellPrice App TimeStamp
0 1 1 2017-10-30 09:45:20 0 9:00:00 2017-10-30 09:45:20
1 1 2 2017-10-30 09:45:38 0 9:00:00 2017-10-30 09:45:38
2 1 3 2017-10-30 09:46:09 0 9:00:00 2017-10-30 09:46:09
3 1 4 2017-10-30 09:46:15 0 9:00:00 2017-10-30 09:46:15
4 1 5 2017-10-30 09:46:17 0 9:00:00 2017-10-30 09:46:17
5 1 1 2017-09-20 09:91:00 99159 9:00:10 2017-09-20 10:31:00
6 1 2 2017-09-20 09:91:02 99058 9:00:11 2017-09-20 10:31:02
7 1 3 2017-09-20 09:91:03 99057 9:00:12 2017-09-20 10:31:03
8 1 4 2017-09-20 09:91:04 99056 9:00:10 2017-09-20 10:31:04
9 1 5 2017-09-20 09:91:05 99055 9:00:10 2017-09-20 10:31:05
10 1 1 2017-09-20 09:91:00 99190 9:01:10 2017-09-20 10:31:00
11 1 2 2017-09-20 09:80:99 99091 9:01:10 2017-09-20 10:21:39
12 1 3 2017-09-20 09:80:98 99092 9:01:10 2017-09-20 10:21:38
13 1 4 2017-09-20 09:80:97 99093 9:01:10 2017-09-20 10:21:37
14 1 5 2017-09-20 09:80:96 99094 9:01:10 2017-09-20 10:21:36
15 2 1 2010-11-01 09:98:90 100000 10:00:02 2010-11-01 10:39:30
16 2 2 2010-11-01 09:98:99 100000 10:00:02 2010-11-01 10:39:39
17 2 3 2010-11-01 09:99:01 99899 9:00:02 2010-11-01 10:39:01
18 2 4 2010-11-01 09:99:20 99850 10:00:02 2010-11-01 10:39:20
19 2 5 2010-11-01 09:99:33 99848 10:00:23 2010-11-01 10:39:33