2
我有以下的熊貓數據框:更換熊貓數據框中的值時出現問題?
在:
df = pd.DataFrame({'Fruits':['this should be a pinneapple',
'this should be an apple',
'this should be a tomato', 'this should 3 grapes',
'this should be an orange',
'this should be an 01',
'this should be an 02']})
df
日期:
Fruits
0 this should be a pinneapple
1 this should be an apple
2 this should be a tomato
3 this should 3 grapes
4 this should be an orange
5 this should be an 01
6 this should be an 02
我想用一個id替換所有的水果(例如01
到nn
)。爲此,我與熊貓試圖替換功能:
df['Fruits'] = df['Fruits'].replace(['pinneapple', 'apple', 'tomato', 'grapes', 'orange'],\
['01', '02', '03', '04', '05'])
然而,當我做上述分配不採取措施的專欄中,我有興趣來調整。因此,如何將每個單詞替換爲預定義的數字?
感謝您的幫助jez! – tumbleweed