Iterating over timestamp in python
The portion of pandas dataframe is given below:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 nan
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 nan
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 nan
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 nan
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 nan
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 nan
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 nan
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 nan
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 nan
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 nan
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 nan
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 nan
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 nan
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 nan
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 nan
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 nan
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 nan
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 nan
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 nan
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 nan
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
I want to get dataframe like below, i.e, to store price value in a different column store_price for rows in the time range 09.20.00 to 09.30.00:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 1237.25
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 1238.30
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 1237.55
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 1237.50
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 1237.05
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 1237.00
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 1237.05
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 1237.00
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 1237.00
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 1236.05
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 1237.00
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 1237.90
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 1237.05
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 1233.50
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 1233.85
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 1234.85
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 1235.45
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 1235.00
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
python-3.x pandas dataframe
add a comment |
The portion of pandas dataframe is given below:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 nan
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 nan
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 nan
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 nan
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 nan
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 nan
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 nan
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 nan
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 nan
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 nan
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 nan
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 nan
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 nan
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 nan
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 nan
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 nan
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 nan
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 nan
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 nan
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 nan
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
I want to get dataframe like below, i.e, to store price value in a different column store_price for rows in the time range 09.20.00 to 09.30.00:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 1237.25
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 1238.30
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 1237.55
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 1237.50
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 1237.05
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 1237.00
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 1237.05
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 1237.00
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 1237.00
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 1236.05
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 1237.00
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 1237.90
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 1237.05
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 1233.50
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 1233.85
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 1234.85
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 1235.45
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 1235.00
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
python-3.x pandas dataframe
1
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
1
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20
add a comment |
The portion of pandas dataframe is given below:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 nan
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 nan
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 nan
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 nan
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 nan
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 nan
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 nan
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 nan
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 nan
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 nan
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 nan
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 nan
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 nan
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 nan
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 nan
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 nan
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 nan
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 nan
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 nan
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 nan
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
I want to get dataframe like below, i.e, to store price value in a different column store_price for rows in the time range 09.20.00 to 09.30.00:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 1237.25
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 1238.30
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 1237.55
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 1237.50
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 1237.05
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 1237.00
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 1237.05
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 1237.00
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 1237.00
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 1236.05
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 1237.00
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 1237.90
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 1237.05
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 1233.50
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 1233.85
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 1234.85
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 1235.45
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 1235.00
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
python-3.x pandas dataframe
The portion of pandas dataframe is given below:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 nan
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 nan
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 nan
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 nan
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 nan
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 nan
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 nan
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 nan
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 nan
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 nan
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 nan
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 nan
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 nan
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 nan
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 nan
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 nan
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 nan
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 nan
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 nan
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 nan
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
I want to get dataframe like below, i.e, to store price value in a different column store_price for rows in the time range 09.20.00 to 09.30.00:
timestamp quantity price Dates Time store_price
2016-07-01 09:15:55 750 1237.50 2016-07-01 09:15:55 nan
2016-07-01 09:16:01 750 1237.35 2016-07-01 09:16:01 nan
2016-07-01 09:16:46 750 1238.15 2016-07-01 09:16:46 nan
2016-07-01 09:16:46 750 1238.00 2016-07-01 09:16:46 nan
2016-07-01 09:18:12 750 1239.70 2016-07-01 09:18:12 nan
2016-07-01 09:19:05 1500 1237.45 2016-07-01 09:19:05 nan
2016-07-01 09:19:58 750 1234.70 2016-07-01 09:19:58 nan
2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95
2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00
2016-07-01 09:20:28 750 1237.25 2016-07-01 09:20:28 1237.25
2016-07-01 09:21:18 750 1238.30 2016-07-01 09:21:18 1238.30
2016-07-01 09:22:29 750 1237.55 2016-07-01 09:22:29 1237.55
2016-07-01 09:22:51 750 1237.50 2016-07-01 09:22:51 1237.50
2016-07-01 09:23:25 750 1237.05 2016-07-01 09:23:25 1237.05
2016-07-01 09:23:28 750 1237.00 2016-07-01 09:23:28 1237.00
2016-07-01 09:24:19 750 1237.05 2016-07-01 09:24:19 1237.05
2016-07-01 09:24:19 2250 1237.00 2016-07-01 09:24:19 1237.00
2016-07-01 09:24:25 750 1237.00 2016-07-01 09:24:25 1237.00
2016-07-01 09:25:23 750 1236.05 2016-07-01 09:25:23 1236.05
2016-07-01 09:26:10 750 1237.00 2016-07-01 09:26:10 1237.00
2016-07-01 09:26:18 750 1237.90 2016-07-01 09:26:18 1237.90
2016-07-01 09:26:25 750 1237.05 2016-07-01 09:26:25 1237.05
2016-07-01 09:27:54 750 1233.50 2016-07-01 09:27:54 1233.50
2016-07-01 09:28:25 750 1233.85 2016-07-01 09:28:25 1233.85
2016-07-01 09:29:17 750 1234.85 2016-07-01 09:29:17 1234.85
2016-07-01 09:29:36 750 1235.45 2016-07-01 09:29:36 1235.45
2016-07-01 09:29:54 750 1235.00 2016-07-01 09:29:54 1235.00
2016-07-01 09:30:06 750 1236.65 2016-07-01 09:30:06 nan
2016-07-01 09:30:36 750 1236.60 2016-07-01 09:30:36 nan
2016-07-01 09:31:01 750 1236.60 2016-07-01 09:31:01 nan
2016-07-01 09:31:09 750 1236.70 2016-07-01 09:31:09 nan
2016-07-01 09:31:15 750 1237.00 2016-07-01 09:31:15 nan
python-3.x pandas dataframe
python-3.x pandas dataframe
edited Nov 23 '18 at 17:58
Julian Peller
864511
864511
asked Nov 14 '18 at 20:09
shalinishalini
42
42
1
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
1
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20
add a comment |
1
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
1
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20
1
1
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
1
1
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20
add a comment |
1 Answer
1
active
oldest
votes
Solution
df['timestamp'] = pd.to_datetime(df['timestamp']) # Only if needed
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Explanation
First, make sure timestamp column is of time datetime:
df['timestamp'].dtypes
If it returns dtype('O'), you need to cast it to datetime using pd.to_datetime, as below:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')
Now you can access the hour and the minute of the column with the .dt accessor and write a mask as below:
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
Finally, you can override the store_price column with price only for the rows that match the condition using .loc:
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Obtaining the results you want.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308031%2fiterating-over-timestamp-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Solution
df['timestamp'] = pd.to_datetime(df['timestamp']) # Only if needed
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Explanation
First, make sure timestamp column is of time datetime:
df['timestamp'].dtypes
If it returns dtype('O'), you need to cast it to datetime using pd.to_datetime, as below:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')
Now you can access the hour and the minute of the column with the .dt accessor and write a mask as below:
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
Finally, you can override the store_price column with price only for the rows that match the condition using .loc:
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Obtaining the results you want.
add a comment |
Solution
df['timestamp'] = pd.to_datetime(df['timestamp']) # Only if needed
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Explanation
First, make sure timestamp column is of time datetime:
df['timestamp'].dtypes
If it returns dtype('O'), you need to cast it to datetime using pd.to_datetime, as below:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')
Now you can access the hour and the minute of the column with the .dt accessor and write a mask as below:
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
Finally, you can override the store_price column with price only for the rows that match the condition using .loc:
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Obtaining the results you want.
add a comment |
Solution
df['timestamp'] = pd.to_datetime(df['timestamp']) # Only if needed
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Explanation
First, make sure timestamp column is of time datetime:
df['timestamp'].dtypes
If it returns dtype('O'), you need to cast it to datetime using pd.to_datetime, as below:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')
Now you can access the hour and the minute of the column with the .dt accessor and write a mask as below:
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
Finally, you can override the store_price column with price only for the rows that match the condition using .loc:
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Obtaining the results you want.
Solution
df['timestamp'] = pd.to_datetime(df['timestamp']) # Only if needed
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Explanation
First, make sure timestamp column is of time datetime:
df['timestamp'].dtypes
If it returns dtype('O'), you need to cast it to datetime using pd.to_datetime, as below:
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')
Now you can access the hour and the minute of the column with the .dt accessor and write a mask as below:
condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30)
Finally, you can override the store_price column with price only for the rows that match the condition using .loc:
df.loc[condition, "store_price"] = df.loc[condition, "price"]
Obtaining the results you want.
edited Nov 23 '18 at 16:14
answered Nov 23 '18 at 15:40
Julian PellerJulian Peller
864511
864511
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308031%2fiterating-over-timestamp-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Hi there, welcome to Stack Overflow! You have provided an example of the dataframe. That's great! You mention about storing data in a different column as well as saving data from a time interval. Can you try to provide another dataframe example of showing the expected series or dataframe? Thanks!
– TrebuchetMS
Nov 15 '18 at 8:35
timestamp quantity price Dates Time store_price 2016-07-01 09:15:09 750 1231.95 2016-07-01 09:15:09 nan 2016-07-01 09:15:28 750 1242.00 2016-07-01 09:15:28 nan 2016-07-01 09:16:26 750 1237.30 2016-07-01 09:16:26 nan 2016-07-01 09:18:48 750 1239.00 2016-07-01 09:18:48 nan 2016-07-01 09:20:02 750 1234.95 2016-07-01 09:20:02 1234.95 2016-07-01 09:20:04 750 1234.00 2016-07-01 09:20:04 1234.00 I was tryingfor (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 13:43
1
Hi again. The comment area isn't suitable for data formatting. Edit your question and put the expected data up there instead?
– TrebuchetMS
Nov 23 '18 at 13:49
I created numpy array ,then tried a 'for' loop, for (datetime.time(9, 15, 0)<timeindex(df[i,4])<datetime.time(9, 30, 0)) df[i,5]=df[i,2] i=i+1
– shalini
Nov 23 '18 at 14:20