我有一个熊猫的数据,如下所示。如何将round和square在shape系列下的值合并为other?(在R术语中,我希望将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发布于 2014-01-23 15:22:53
你是这个意思吗?
df.loc[df['shape'].isin(['round', 'square']), 'shape'] = 'other'(在@TomAugspurger的建议之后编辑)
https://stackoverflow.com/questions/21312266
复制相似问题