python - Pandas:如果字符串列表中不存在,则将其替换为 'other'

标签 python python-3.x pandas

我有以下数据框 df,列为“Class”

    Class
0   Individual
1   Group
2   A
3   B
4   C
5   D
6   Group

我想用“Other”替换除 Group 和 Individual 之外的所有内容,所以最终的数据框是

    Class
0   Individual
1   Group
2   Other
3   Other
4   Other
5   Other
6   Group

数据框很大,有超过 60 万行。以最佳方式查找“Group”和“Individual”以外的值并将其替换为“Other”的最佳方法是什么?

我看过replace的例子,比如:

df['Class'] = df['Class'].replace({'A':'Other', 'B':'Other'})

但由于我拥有的唯一值数量太多,我无法单独执行此操作。我只想使用“组”和“个人”的排除子集。

最佳答案

我认为你需要:

df['Class'] = np.where(df['Class'].isin(['Individual','Group']), df['Class'], 'Other')
print (df)
        Class
0  Individual
1       Group
2       Other
3       Other
4       Other
5       Other
6       Group

另一种解决方案(较慢):

m = (df['Class'] == 'Individual') | (df['Class'] == 'Group')
df['Class'] = np.where(m, df['Class'], 'Other')

另一种解决方案:

df['Class'] = df['Class'].map({'Individual':'Individual', 'Group':'Group'}).fillna('Other')

性能(实际数据取决于替换次数):

#[700000 rows x 1 columns]
df = pd.concat([df] * 100000, ignore_index=True)
#print (df)

In [208]: %timeit df['Class1'] = np.where(df['Class'].isin(['Individual','Group']), df['Class'], 'Other')
25.9 ms ± 485 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [209]: %timeit df['Class2'] = np.where((df['Class'] == 'Individual') | (df['Class'] == 'Group'), df['Class'], 'Other')
120 ms ± 6.63 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [210]: %timeit df['Class3'] = df['Class'].map({'Individual':'Individual', 'Group':'Group'}).fillna('Other')
95.7 ms ± 3.85 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [211]: %timeit df.loc[~df['Class'].isin(['Individual', 'Group']), 'Class'] = 'Other'
97.8 ms ± 6.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

关于python - Pandas:如果字符串列表中不存在,则将其替换为 'other',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51323038/

相关文章:

python - 使用 Scikit Learn 进行 Unigram 分析

python - 非关键字标识符的正则表达式

python - 当我在图像上打印文本并且它超出了图像的框架时,如何在 OpenCV 中包装文本?

python - pandas - 指数加权移动平均线 - 类似于 excel

python - 计算 Pandas 中某个类别的百分比

python - UDP 客户端服务器应用程序的 Errno 61 连接被拒绝

带有嵌套列表的 Python 字典键到 pandas DataFrame

python - Matplotlib savefig 截断pyplot表

Python Serial Read 返回奇怪的值

Python 调用 - 找不到任何名为 'tasks' 的集合!