pandas - 将数据映射到地面实况列表
问题描述
我在以下 Python 列表中有基本事实数据:
ground_truth = [(A,16), (B,18), (C,36), (A,59), (C,77)]
所以任何值来自:
0-16 gets mapped to A,
17-18 maps to B,
19-36 maps to C,
37-59 maps to A
60-77 maps to C
and so on
我正在尝试从像这样的数字映射时间序列输入
[9,15,29,32,49,56, 69] to its respective classes like:
[A, A, C, C, A, A, C]
假设我的输入是 Pandas 系列,例如:
in = pd.Series([9,15,29,32,49,56, 69])
我如何进入该系列[A, A, C, C, A, A, C]
?
解决方案
这是我的方法:
gt = pd.DataFrame(ground_truth)
# bins for cut
bins = [0] + list(gt[1])
# categories
cats = pd.cut(pd.Series([9,15,29,32,49,56, 69]), bins=bins, labels=False)
# labels
gt.loc[cats, 0]
给
0 A
0 A
2 C
2 C
3 A
3 A
4 C
Name: 0, dtype: object
或者,不创建新数据框:
labels = np.array([x for x,_ in ground_truth])
bins = [0] + [y for _,y in ground_truth]
cats = pd.cut(pd.Series([9,15,29,32,49,56, 69]), bins=bins, labels=False)
labels[cats]
这使:
array(['A', 'A', 'C', 'C', 'A', 'A', 'C'], dtype='<U1')
推荐阅读
- angular - 响应属性问题
- azure-devops - 运行赛普拉斯测试后未找到 Azure DevOps 测试 -xml
- reactjs - 在 React Hook useCallback 中,(a,b) 是如何使用的
- git - Git pull 覆盖并且不合并或确认同一分支(主)上的冲突
- excel - 添加 nlog 后 Excel 插件不再工作(与 addin-express 一起安装)
- javascript - React : Invariant Violation: 对象作为 React 子对象无效
- python - Pyinstaller EXE: FileNotFoundError: No such file or directory 'C:\\Users\\my.name\\Desktop\\EPE 2.0\\dist\\main\\timezonefinder\\timezone_names.json'
- r - 字符串匹配,其中字符串包含标点符号
- bash - bash:基本的 if 条件不起作用?(字符串比较)
- javascript - 如何触发自定义复选框小部件