python - 以系列结尾的 Pandas OR 语句包含

标签 python pandas

我有一个 DataFrame df,它有列 typesubtype 以及大约 100k 行,我正在尝试对哪种数据进行分类df 通过检查 type/subtype 组合包含。虽然 df 可以包含许多不同的组合,但有一些特定的组合只出现在特定的数据类型中。要检查我的对象是否包含我目前正在做的任何这些组合:

typeA = ((df.type == 0) & ((df.subtype == 2) | (df.subtype == 3) | 
         (df.subtype == 5) | (df.subtype == 6))) | 
         ((df.type == 5) & ((df.subtype == 3) | (df.subtype == 4) | (df.subtype == 7) | 
         (df.subtype ==  8)))
A = typeA.sum()

其中 typeA 是一长串可能有一些真值的假值,如果 A > 0 那么我知道它包含一个真值。这个方案的问题在于,如果 df 的第一行产生一个 True,它仍然必须检查其他所有内容。检查整个 DataFrame 比使用带中断的 for 循环更快,但我想知道是否有更好的方法来做到这一点。

感谢您的任何建议。

最佳答案

使用Pandas crosstab :

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0, 10, size=(100, 2)), columns=["type", "subtype"])
counts = pd.crosstab(df.type, df.subtype)

print counts.loc[0, [2, 3, 5, 6]].sum() + counts.loc[5, [3, 4, 7, 8]].sum()

结果是一样的:

a = (((df.type == 0) & ((df.subtype == 2) | (df.subtype == 3) | 
         (df.subtype == 5) | (df.subtype == 6))) | 
         ((df.type == 5) & ((df.subtype == 3) | (df.subtype == 4) | (df.subtype == 7) | 
         (df.subtype ==  8))))
a.sum()

关于python - 以系列结尾的 Pandas OR 语句包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062684/

相关文章:

python - 如何根据列的过滤条件删除行

python - Pandas 检查列是否是类别问题

python - numpy 数组的矢量化 "by-layer"缩放

python - 无法使用 python-requests 发布文件+数据

python - 使用 Mechanize 访问 Javascript 按钮

python - 将数据分成连续的组

python - 遍历 Python 查询字符串中的日期列表

python - 使用来自 pandas DataFrame 的数据拟合 sklearn 的 SVM 分类器

python pandas 在最后一个非 NaN 值处停止 fillna

python - 使 LiveServerTestCase 在每次测试之前不调用 setUp()