首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >熊猫:合并系列价值

熊猫:合并系列价值
EN

Stack Overflow用户
提问于 2014-01-23 15:19:08
回答 1查看 368关注 0票数 0

我有一个熊猫的数据,如下所示。如何将roundsquareshape系列下的值合并为other?(在R术语中,我希望将roundsquare级别的shape因子合并为一个新的级别,称为other。)

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-23 15:22:53

你是这个意思吗?

代码语言:javascript
复制
df.loc[df['shape'].isin(['round', 'square']), 'shape'] = 'other'

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21312266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档