首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当熊猫在列中相同的时候,如何使用它们来重命名行?

当熊猫在列中相同的时候,如何使用它们来重命名行?
EN

Stack Overflow用户
提问于 2021-12-30 08:57:42
回答 2查看 213关注 0票数 0

根据:

How to use pandas to rename rows when they are the same in column A?

我的数据是:

当医院列中值相同的行在GeneralRepresentation列中有不同的值时,我想使用熊猫来重命名医院。如果医院列中值相同的行在GeneralRepresentation列中具有相同的值,则不会对医院进行重命名。对于没有GeneralRepresentation的医院,保持医院的名称不变。

我想要的效果如下:

当我在How to use pandas to rename rows when they are the same in column A?中使用Beny的代码时

代码语言:javascript
复制
g = df.groupby('Hospital')['GeneralRepresentation']
s1 = g.transform(lambda x :x.factorize()[0]+1).astype(str)
s2 = g.transform('nunique')
df['Hospital'] = np.where(s2==1, df['Hospital'], df['Hospital'] + '_' + s1,)

其效果如下:

但是我想要的是当医院没有GeneralRepresentation时,医院的名称保持不变,效果就像第二张图片,我如何修改这个代码以满足我的要求?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-30 09:11:30

问题是缺少值,因为misisng值是factorize设置为-1,所以如果为最后2行添加1 get 0,则在我的解决方案中,将NaN替换为groupby之前的空字符串,以防止出现这样的情况:

代码语言:javascript
复制
g = df.fillna({'GeneralRepresentation':''}).groupby('Hospital')['GeneralRepresentation']
s1 = g.transform(lambda x :x.factorize()[0]+1).astype(str)
s2 = g.transform('nunique')
df['Hospital'] = np.where(s2==1, df['Hospital'], df['Hospital'] + '_' + s1)
print (df)
  Hospital GeneralRepresentation
0        a                     a
1      b_1                     b
2      b_2                     c
3      c_1                     d
4      c_2                     e
5        d                   NaN
6        t                   NaN
票数 1
EN

Stack Overflow用户

发布于 2021-12-30 09:18:42

使用np.select(listof conditions, list of choices, alternative)

代码语言:javascript
复制
a=~(df['GeneralRepresentation'].str.contains('\w'))
b= ((df['GeneralRepresentation'].str.contains('\w'))&(df['Hospital'].duplicated(keep=False))&(df['GeneralRepresentation'].duplicated(keep=False)))

df['Hospital'] np.select([a,b],[df['Hospital']+'_'+(df.groupby('Hospital').cumcount()+1).astype(str),''],df['Hospital'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70529415

复制
相关文章

相似问题

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