【发布时间】: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