如何有效地将 pandas 系列(或索引级别)平铺到给定数组(系列、索引),即映射每个元素 x
系列中的最大元素y
在地板阵列中,这样 y <= x
?
这是一个例子:
import pandas as pd
# the series
x = pd.Series([1.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1, 11.11])
# the floor array
y = pd.Series([1.0, 4.0, 10.0])
# expected result (can be a numpy array, a Series, a list, etc...)
z = [1.0, 1.0, 1.0, 1.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 10.0, 10.0]
您可以假设系列和数组都按升序排序。
最佳答案
尝试pd.cut
:
pd.cut(x, bins=list(y)+[np.inf], right=False, labels=y).astype(float)
输出:
0 1.0
1 1.0
2 1.0
3 1.0
4 4.0
5 4.0
6 4.0
7 4.0
8 4.0
9 4.0
10 10.0
11 10.0
dtype: float64
关于python - 将 Series 平铺到给定数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327623/