如何在 lambda 中应用映射?
How to apply a map in a lambda?
我在数据集中有一个名为“Increasing_sequence”的列:
dict = {"Increasin_Sequence": [[[0.98, 1.1, 1.25], [1.18, 1.28]],[[1.2, 1.2], [1.1, 1.25]],[[0.85, 1.2, 1.29, 1.31, 1.4]],
[[1.19, 1.29, 1.39, 1.49]], [[1.0, 1.0, 1.0, 1.0]]] }
dt = pd.DataFrame(dict)
Increasin_Sequence
0 [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.0, 1.2], [1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]]
4 [[1.0, 1.0, 1.0, 1.0]]
每一列都由一个“非递减”列表组成。我想保留满足以下两个要求的列表(在名为“spikes”的新列中):
1-[最大数]-[最小数]>=.1
2-[最大数] > 1.2
所以期望的输出可能如下所示:
spikes
0 [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]]
4 []
我开发了以下代码:
def spikes_finder(dt):
dt['IncreasingSequences'].apply(lambda x: map(apply_spike_conditions, x))
def apply_spike_conditions(increasing_sequence ):
pick = increasing_sequence[-1]
valley = increasing_sequence[0]
pick_to_valley_difference = pick - valley
if (pick_to_valley_difference >= .1) and (pick > 1.2):
return increasing_sequence
运行 这段代码,apply
函数没有执行,我还尝试使用 for 循环,这不是一种有效的方法,所以我宁愿使用 apply 或 map 函数
总的来说,我觉得把步骤写成函数更容易,这样更容易理解,例如:
def conditions(lst):
"""This functions checks the filter conditions"""
mi, *_, ma = lst # extract the first and the last
return ma > 1.2 and ma - mi > 0.1
def filter_cells(cell):
"""This function simply applies the filters"""
return [lst for lst in cell if conditions(lst)]
dt['filtered'] = dt['Increasing_Sequence'].apply(filter_cells)
print(dt)
输出
Increasing_Sequence filtered
0 [[0.98, 1.1, 1.25], [1.18, 1.28]] [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.2, 1.2], [1.1, 1.25]] [[1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]] [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]] [[1.19, 1.29, 1.39, 1.49]]
4 [[1.0, 1.0, 1.0, 1.0]] []
符号
mi, *_, ma = lst
被称为 extended iterable unpacking。它这个上下文可以理解为给我第一个(mi
),忘记中间(*_
)并给我最后一个元素(ma
)。
关于您的功能,我认为您在 spikes_finder 中缺少一个 return,也许 return [=16] 更好=] 或 False
in apply_spike_conditions 并使用 filter 而不是 map.
请注意,在 Python 3 中,map 和 filter 都是 return 可迭代对象,因此您需要将结果转换为列表。
我在数据集中有一个名为“Increasing_sequence”的列:
dict = {"Increasin_Sequence": [[[0.98, 1.1, 1.25], [1.18, 1.28]],[[1.2, 1.2], [1.1, 1.25]],[[0.85, 1.2, 1.29, 1.31, 1.4]],
[[1.19, 1.29, 1.39, 1.49]], [[1.0, 1.0, 1.0, 1.0]]] }
dt = pd.DataFrame(dict)
Increasin_Sequence
0 [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.0, 1.2], [1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]]
4 [[1.0, 1.0, 1.0, 1.0]]
每一列都由一个“非递减”列表组成。我想保留满足以下两个要求的列表(在名为“spikes”的新列中):
1-[最大数]-[最小数]>=.1
2-[最大数] > 1.2
所以期望的输出可能如下所示:
spikes
0 [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]]
4 []
我开发了以下代码:
def spikes_finder(dt):
dt['IncreasingSequences'].apply(lambda x: map(apply_spike_conditions, x))
def apply_spike_conditions(increasing_sequence ):
pick = increasing_sequence[-1]
valley = increasing_sequence[0]
pick_to_valley_difference = pick - valley
if (pick_to_valley_difference >= .1) and (pick > 1.2):
return increasing_sequence
运行 这段代码,apply
函数没有执行,我还尝试使用 for 循环,这不是一种有效的方法,所以我宁愿使用 apply 或 map 函数
总的来说,我觉得把步骤写成函数更容易,这样更容易理解,例如:
def conditions(lst):
"""This functions checks the filter conditions"""
mi, *_, ma = lst # extract the first and the last
return ma > 1.2 and ma - mi > 0.1
def filter_cells(cell):
"""This function simply applies the filters"""
return [lst for lst in cell if conditions(lst)]
dt['filtered'] = dt['Increasing_Sequence'].apply(filter_cells)
print(dt)
输出
Increasing_Sequence filtered
0 [[0.98, 1.1, 1.25], [1.18, 1.28]] [[0.98, 1.1, 1.25], [1.18, 1.28]]
1 [[1.2, 1.2], [1.1, 1.25]] [[1.1, 1.25]]
2 [[0.85, 1.2, 1.29, 1.31, 1.4]] [[0.85, 1.2, 1.29, 1.31, 1.4]]
3 [[1.19, 1.29, 1.39, 1.49]] [[1.19, 1.29, 1.39, 1.49]]
4 [[1.0, 1.0, 1.0, 1.0]] []
符号
mi, *_, ma = lst
被称为 extended iterable unpacking。它这个上下文可以理解为给我第一个(mi
),忘记中间(*_
)并给我最后一个元素(ma
)。
关于您的功能,我认为您在 spikes_finder 中缺少一个 return,也许 return [=16] 更好=] 或 False
in apply_spike_conditions 并使用 filter 而不是 map.
请注意,在 Python 3 中,map 和 filter 都是 return 可迭代对象,因此您需要将结果转换为列表。