我想知道是否有办法更改pandas dataframe中的类别名称。我尝试使用labels.rename_categories({'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'})
,但遗憾的是它没有起作用。
下面是当前pandas dataframe的样子:
File Label
20936 eight/b63fea9e_nohash_1.wav eight
21016 eight/f44f440f_nohash_2.wav eight
7423 three/d8ed3745_nohash_0.wav three
1103 zero/ad63d93c_nohash_4.wav zero
13399 five/5b09db89_nohash_0.wav five
... ... ...
13142 five/1a892463_nohash_0.wav five
21176 eight/810c99be_nohash_0.wav eight
16908 seven/6d818f6c_nohash_0.wav seven
15308 six/2bfe70ef_nohash_1.wav six
646 zero/24632875_nohash_0.wav zero
[23666 rows x 2 columns]
df['Labels'] = df['Labels'].replace({ 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9' } )
。我使用了df['Labels']
来避免用重命名的类别覆盖数据框中的其余部分。 - Loai Alnouri