【问题标题】:Replace values of unmatched index with 'Other' python3用“其他”python3替换不匹配索引的值
【发布时间】:2019-03-05 10:21:17
【问题描述】:

这是原始系列。我正在尝试用“其他”替换系列中非前 2 名的值。

原创系列(ser3):

 b    8
 c    6
 a    5 
 h    4
 g    2
 d    2
 f    2
 e    1

这是我提取的前 2 名。

前 2 名:

t2 = ((ser3.value_counts().head(2)))

b    8
c    6

预期输出:

 b    8
 c    6
 a    Other 
 h    Other
 g    Other
 d    Other
 f    Other
 e    Other

我该怎么做?我不想转换为字典并通过索引替换值。我更喜欢按系列来做。我尝试使用 .isin,但我的代码给了我一个错误。

 a[a[~a.isin(t2)].index]='Other'

上面给了我一个错误。

【问题讨论】:

  • 你确定要混合这样的数据类型吗?

标签: python-3.x pandas series


【解决方案1】:

你很接近,需要选择t2.index并删除外部ser3[]

ser3[~ser3.isin(t2.index)]='Other'

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2016-05-23
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    相关资源
    最近更新 更多