熊猫:合并系列值

时间:2014-01-23 15:19:09

标签: python pandas

我有一个Pandas数据帧,如下所示。如何将round系列下的squareshape值合并为other? (在R术语中,我想将round因子的squareshape级别合并到标记为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

1 个答案:

答案 0 :(得分:2)

这是什么意思?

df.loc[df['shape'].isin(['round', 'square']), 'shape'] = 'other'

(在@ TomAugspurger的建议之后编辑)