我在以下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]
假设我输入的是熊猫系列,例如:
in = pd.Series([9,15,29,32,49,56, 69])
如何进入[A, A, C, C, A, A, C]
系列?
答案 0 :(得分:6)
这是我的方法:
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')