You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计数据集中高值共现的列对及列三元组?

解决高值共现列组合统计问题

Got it, let's break down how to solve this problem. You need to count how often pairs, triplets (or even larger groups) of columns have high values (4, 5, 6) in the same row—where every column in the group must be a high value for that row to count. Below's a practical, code-based solution using Python and pandas, which is ideal for this kind of tabular data analysis:

步骤1:预处理数据,生成高值掩码矩阵

首先,我们把原始数据转换成一个布尔矩阵,标记每个单元格是否属于高值(4、5、6)。这一步能简化后续的共现判断:

import pandas as pd
import itertools

# 读取你的数据集(替换成你的数据路径/读取方式)
df = pd.read_csv("your_dataset.csv")

# 创建高值掩码:True表示该单元格是高值,False则不是
high_value_mask = df.isin([4, 5, 6])

步骤2:统计列对的高值共现次数

遍历所有可能的两列组合,统计同一行中两列均为高值的行数:

# 存储列对的共现次数
pair_cooccurrences = {}

# 生成所有列对组合
for col1, col2 in itertools.combinations(df.columns, 2):
    # 统计同一行中col1和col2全为高值的行数
    count = high_value_mask[[col1, col2]].all(axis=1).sum()
    # 只保留有共现记录的列对
    if count > 0:
        pair_cooccurrences[(col1, col2)] = count

# 按共现次数降序输出结果
print("=== 高值共现列对统计 ===")
for pair, cnt in sorted(pair_cooccurrences.items(), key=lambda x: x[1], reverse=True):
    print(f"{pair[0]} & {pair[1]}: {cnt} 次")

步骤3:统计列三元组的高值共现次数

用同样的逻辑处理三列组合,统计同一行中三列均为高值的行数:

# 存储列三元组的共现次数
triplet_cooccurrences = {}

# 生成所有列三元组组合
for cols in itertools.combinations(df.columns, 3):
    # 统计同一行中这三列全为高值的行数
    count = high_value_mask[list(cols)].all(axis=1).sum()
    if count > 0:
        triplet_cooccurrences[cols] = count

# 按共现次数降序输出结果
print("\n=== 高值共现列三元组统计 ===")
for triplet, cnt in sorted(triplet_cooccurrences.items(), key=lambda x: x[1], reverse=True):
    print(f"{', '.join(triplet)}: {cnt} 次")

额外说明

  • 如果需要统计更多列的组合(比如4列、5列),只需要修改itertools.combinations的第二个参数(比如改成4就生成所有4列组合),但要注意列数越多,组合数量会指数级增长,建议根据实际需求筛选。
  • all(axis=1)是关键:它会检查每行中指定的所有列是否都为True(即全是高值),sum()则统计满足条件的行数,也就是共现次数。

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:36:51