0
我有一个熊猫数据框如下。我怎样才能将round
和square
的值合并为shape
系列作为other
? (在读的术语,我想将round
和square
水平shape
因素合并成标other
一个新的水平。)熊猫:合并系列值
df = pd.DataFrame({'id' : range(1,9),
'code' : ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'shape': ['round', 'triangular', 'triangular','triangular','square',
'triangular','round','triangular'],
'amount' : np.random.randn(8)}, columns= ['id','code', 'shape', 'amount'])
df
id code shape amount
0 1 one round -0.187789
1 2 one triangular 1.286208
2 3 two triangular 0.171734
3 4 three triangular 0.394471
4 5 two square -0.009613
5 6 three triangular 0.413767
6 7 one round 1.264730
7 8 two triangular 0.516499
是(在@ TomAugspurger的建议编辑),我相信如此。熊猫系列没有水平,所以我不必担心下降水平('圆形和'方形),对吧? – Rhubarb
他们是字符串,所以我不认为你应该担心 – mkln
您可能想要将其更改为'df.loc [df ['shape']。isin(['round','square']),'shape' ] ='其他',以避免复制错误的可能设置。这里似乎不成问题,但最好避免链接分配。 – TomAugspurger