为每个集群(区域)的所有空时间箱填写零,作为救护车响应/发布分析的一部分



我有一个来自机构救护车响应的数据。 我为他们响应的位置创建了集群,并创建了时间箱以将时间划分为可管理的箱。 然后,我先按群集和 bin 对数据进行分组,并将每个 bin 的每个群集的事件数也聚合到一列中。 我需要为事件计数列中未发生事件的所有时间箱填写零。我已经尝试了一个带有 if else 的嵌套 for 循环来使其工作。 它运行得太慢了,我正在尝试找到一种方法来切换到带有 if else 语句的嵌套列表理解。

count_values = ers_bin_groupby['no_of_incidents'].values
vals = ers_unique 

"ers_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_values

这是我正在使用的分组数据的示例,有 20 个帖子和超过 500,000 个时间箱

post_area  time_bin    
0             7      1
59     1
104    1
113    1
117    1
147    1
249    1
255    1

这是ers_unique列表的示例 [[7, 59, 104, 113, 117, 147, 249, 255, 277, 283, 292, 310, 312, 358, 393, 406, 480, 537, 550, 553, 622,

除了ind += 1问题之外,请注意,smoothed_regions.extend(smoothed_bins)意味着您返回一个平面列表,而不是一个smoothed_bins列表(就像您的尝试一样(。

如果没有示例输入/输出,我无法测试下面的代码,但我希望没问题。

首先,用迭代替换索引:

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]

最新更新