在python中的时间戳上进行迭代

时间:2018-11-14 20:09:29

标签: python-3.x pandas dataframe

熊猫数据框的一部分如下:

 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

我想获得如下数据框,即将price的值存储在时间范围为09.20.00到09.30.00的行的不同列store_price中:

 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

1 个答案:

答案 0 :(得分:0)

解决方案

function print() {
  if(xhr.readyState === 4 && xhr.status === 200) {
     console.log(xhr.responseText);
   }
}

说明

首先,确保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"] 列的时间为timestamp

datetime

如果它返回df['timestamp'].dtypes ,则需要使用pd.to_datetime将其强制转换为日期时间,如下所示:

dtype('O')

现在,您可以使用.dt访问器访问列的小时和分钟,并编写如下掩码:

df['timestamp'] = pd.to_datetime(df['timestamp'])
df['timestamp'].dtypes
>>> dtype('<M8[ns]')

最后,您只能使用.loc对符合条件的行用condition = (df['timestamp'].dt.hour == 9) & (df['timestamp'].dt.minute >= 20) & (df['timestamp'].dt.minute <= 30) 覆盖store_price列:

price

获得所需的结果。