1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
由after?withoutSeries.map
的索引創(chuàng)建的字典使用:Series
Series.value_counts
-1
s = example['y'].value_counts().drop(-1)
d = {v:k for k, v in dict(enumerate(s.index)).items()}
或者:
s = example['y'].value_counts().drop(-1)
d = dict(zip(s.index, range(len(s))))
m = example['y'].ne(-1)
example.loc[m, 'y'] = example.loc[m, 'y'].map(d)
print (example)
? y
0? ?0
1? ?0
2? ?0
3? ?0
4? ?0
5? ?0
6? ?0
7? ?0
8? ?0
9? ?0
10? 1
11? 1
12? 1
13? 1
14? 2
15? 2
16 -1
17 -1
18 -1
另一個(gè)想法是增加-1價(jià)值:-1dictionary
s = example['y'].value_counts().drop(-1)
d = {**{-1:-1}, **dict(zip(s.index, range(len(s))))}
example['y'] = example['y'].map(d)
添加回答
舉報(bào)