Python 作为救护车响应/张贴分析的一部分,为每个集群(区域)的所有空时间箱填写零
我从一家机构的救护车上得到了一份我的数据。我为他们响应的地点创建了集群,并创建了时间箱,将时间划分为可管理的时间箱。然后,我按集群和bin对数据进行分组,并将每个集群每个bin的事件数聚合到一列中。我需要在事件计数列中为没有发生事件的所有时间段填入零。我尝试了一个嵌套的for循环,并使用if-else使其工作。它运行得太慢,我正试图找到一种方法,用if-else语句切换到嵌套的列表理解Python 作为救护车响应/张贴分析的一部分,为每个集群(区域)的所有空时间箱填写零,python,for-loop,if-statement,time-series,list-comprehension,Python,For Loop,If Statement,Time Series,List Comprehension,我从一家机构的救护车上得到了一份我的数据。我为他们响应的地点创建了集群,并创建了时间箱,将时间划分为可管理的时间箱。然后,我按集群和bin对数据进行分组,并将每个集群每个bin的事件数聚合到一列中。我需要在事件计数列中为没有发生事件的所有时间段填入零。我尝试了一个嵌套的for循环,并使用if-else使其工作。它运行得太慢,我正试图找到一种方法,用if-else语句切换到嵌套的列表理解 count_values = ers_bin_groupby['no_of_incidents'].value
count_values = ers_bin_groupby['no_of_incidents'].values
vals = ers_unique
“ERSU unique”是每个集群的所有唯一时间单元的列表
def fill_missing(count_values,vals):
smoothed_regions=[]
ind=0 # ind iterates over count_values only
for p in range(0,posts):
smoothed_bins=[]
for i in range(max(minute_bin_create_times)):
if i in vals[p]:
smoothed_bins.append(count_values[ind])
ind+=1
else:
smoothed_bins.append(0)
smoothed_regions.extend(smoothed_bins)
print(p)
return smoothed_regions
这是我尝试用if语句理解列表
def fill_missing2(count_values, vals):
smoothed_regions = []
ind = 0 #ind iterates over count_values only
smoothed_regions =[[count_values[ind] ind+=1 if i in vals[p] else 0
for i in range(max(minute_bin_create_times))]
for p in range(0,posts)]
我不知道我是否仍然需要“ind+=1”使其通过count_值
这是我正在处理的groupby数据的一个例子,有20篇文章和超过500000个时间段
post_area time_bin
0 7 1
59 1
104 1
113 1
117 1
147 1
249 1
255 1
这是ers_唯一列表的一个示例
[[7,59,104,113,117,147,249,255,277,283,292,310,312,358,393,406,480,537,550,553,622,除了
ind+=1
问题之外,请注意平滑的\u区域。扩展(平滑的\u区域)
意味着返回一个平面列表,而不是平滑的\u区域列表(如您尝试的那样)
没有示例输入/输出,我无法测试下面的代码,但我希望它可以
首先,用迭代替换索引:
def fill_missing(count_values,vals):
smoothed_regions=[]
count_it = iter(count_values) # iterates over count_values only
for val in vals: # I assume that posts == len(vals)
smoothed_bins=[]
for i in range(max(minute_bin_create_times)):
if i in val:
smoothed_bins.append(next(count_it))
else:
smoothed_bins.append(0)
smoothed_regions.extend(smoothed_bins)
return smoothed_regions
然后,将if…append…else…append…
部分替换为append(…if…else…
),即:使用表达式而不是语句:
def fill_missing(count_values,vals):
smoothed_regions=[]
count_it = iter(count_values) # iterates over count_values only
for val in vals:
smoothed_bins=[]
for i in range(max(minute_bin_create_times)):
smoothed_bins.append(next(count_it) if i in val else 0)
smoothed_regions.extend(smoothed_bins)
return smoothed_regions
现在,您可以创建第一个列表:
def fill_missing(count_values,vals):
smoothed_regions=[]
count_it = iter(count_values) # iterates over count_values only
for val in vals[:posts]: # posts == len(vals)??
smoothed_bins=[next(count_it) if i in val else 0
for i in range(max(minute_bin_create_times))]
smoothed_regions.extend(smoothed_bins)
return smoothed_regions
然后,您需要构建一个所有平滑绑定的列表,并将其展平(extend
)。
实际上,生成器(对我来说)似乎比列表更适合:
def fill_missing(count_values,vals):
count_it = iter(count_values) # iterates over count_values only
all_smoothed_bins = ([next(count_it) if i in val else 0
for i in range(max(minute_bin_create_times))] for val in vals[:posts])
smoothed_regions = [v for smoothed_bins in all_smoothed_bins for v in smoothed_bins]
return smoothed_regions
如果需要,可以内联最后三行:
def fill_missing(count_values,vals):
count_it = iter(count_values) # iterates over count_values only
return [v
for smoothed_bins in (
[next(count_it) if i in val else 0
for i in range(max(minute_bin_create_times))]
for val in vals[:posts]
)
for v in smoothed_bins]