标识数据帧中任何相等元素组合的出现次数

2024-09-27 07:34:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个模拟数据框,df1有6列5行,也就是说,有形状(5 x 6)

每列表示一个区域的价格,行表示时间

   DK1   DK2   NO1   NO2   NO3   NO4
0  10    10    12    15    15    10
1  15    10    15    10    10    10
2  15    15    15    15    15    15
3  10    10    12    15    15    10
4  10    10    10    10    15    15

现在,我想在每一行中确定与第一列“DK1”价格相同的区域,然后能够总结出任何列组合相等的频率,这意味着我对此的期望输出为:

       Combo                  Occurrence
0   DK1-DK2-NO4                   2
1   DK1-DK2-NO1-NO2-NO3-NO4       1
2   DK1-NO1                       1
3   DK1-DK2-NO1-NO2               1

优选地,该解决方案应适用于任何大小的数据帧。我开始尝试使用.apply()方法,但无法完全开始。希望你能帮忙


Tags: 数据区域时间价格频率形状df1combo
1条回答
网友
1楼 · 发布于 2024-09-27 07:34:23

通过^{}按第一列比较数据帧,然后使用^{}与带分隔符的列名进行矩阵相乘,使用^{}进行最后计数,并转换为DataFrame

df = (df.eq(df['DK1'], axis=0)
        .dot(df.columns + ',')
        .str[:-1]
        .value_counts()
        .rename_axis('Combo')
        .reset_index(name='Occurrence'))
print (df)
                     Combo  Occurrence
0              DK1,DK2,NO4           2
1                  DK1,NO1           1
2          DK1,DK2,NO1,NO2           1
3  DK1,DK2,NO1,NO2,NO3,NO4           1

编辑:对于组,可以通过所有值创建字典,然后调用replace

s = df.columns.to_series()
s.index = s.index.str.replace('\d+','', regex=True)

d = s.groupby(level=0).agg(','.join).to_dict()
d = {v:k for k, v in d.items()}
print (d)
{'DK1,DK2': 'DK', 'NO1,NO2,NO3,NO4': 'NO'}

df = (df.eq(df['DK1'], axis=0)
        .dot(df.columns + ',')
        .str[:-1]
        .value_counts()
        .rename_axis('Combo')
        .reset_index(name='Occurrence'))

df['Combo'] = df['Combo'].replace(d, regex=True)
print (df)
        Combo  Occurrence
0      DK,NO4           2
1     DK1,NO1           1
2  DK,NO1,NO2           1
3       DK,NO           1

相关问题 更多 >

    热门问题