Note
Pandas (10) 본문
728x90
How to keep only top 2 most frequent values as it is and replace everything else as ‘Other’?
# Input
np.random.RandomState(100)
ser = pd.Series(np.random.randint(1, 5, [12]))
# Solution
print("Top 2 Freq:", ser.value_counts())
ser[~ser.isin(ser.value_counts().index[:2])] = 'Other'
'Pandas' 카테고리의 다른 글
Pandas (12) (0) | 2022.08.02 |
---|---|
Pandas (11) (0) | 2022.07.31 |
Pandas (9) (0) | 2022.07.29 |
Pandas (8) (0) | 2022.07.28 |
Pandas (7) (0) | 2022.07.27 |
Comments