Pandas 将数据映射到地面真相列表

Pandas 将数据映射到地面真相列表,pandas,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 r

我在下面的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]

以下是我的方法:

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]
给予

或者,在不创建新数据帧的情况下:

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')
array(['A','A','C','C','A','A','C'],dtype='
array(['A', 'A', 'C', 'C', 'A', 'A', 'C'], dtype='<U1')