python - 将系列放置到给定数组
问题描述
如何有效地将熊猫系列(或索引级别)放置到给定数组(系列,索引),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 - 在 Django 中输出 CSV 不能自动工作
- javascript - 我正在尝试使用 CSS 实现图像悬停效果。我希望标题或图片标题在悬停时可见
- php - v-on 处理程序中的错误(Promise/async):“TypeError:无法读取未定义的属性 'push'”
- list - 如何在 Python 中以特定格式调用所有列表元素?
- rundeck - 更改 Rundeck 存储密码
- javascript - 有没有办法根据类索引改变变量?
- python - 删除 QLabel 和 QLineEdit 之间的边距
- windows - 我可以创建一个 pid 为 8 的进程吗?
- javascript - 选择烧瓶后未显示下拉选择值?
- javascript - 如何解决此“离子服务”错误?