Python 将集合。组合频率计数器从数据帧多索引转换为字符串

Python 将集合。组合频率计数器从数据帧多索引转换为字符串,python,pandas,dictionary,collections,multi-index,Python,Pandas,Dictionary,Collections,Multi Index,我想就如何正确地做到这一点征求一些建议。我是python新手 最初我想找出多索引组合的计数器/频率。我尝试了一些方法,如循环、itertuples、iterrows等,我意识到最快和最小的开销是使用集合 但是,它返回多索引组合的元组列表作为计数器dict键。元组的键使得后续处理变得困难 因此,我正在研究如何使用分隔符将它们制成字符串,以使后续处理更易于管理 例如,下面的多索引: # testing def testing(): testing_df = pd.read_csv("

我想就如何正确地做到这一点征求一些建议。我是python新手

最初我想找出多索引组合的计数器/频率。我尝试了一些方法,如循环、itertuples、iterrows等,我意识到最快和最小的开销是使用集合

但是,它返回多索引组合的元组列表作为计数器dict键。元组的键使得后续处理变得困难

因此,我正在研究如何使用分隔符将它们制成字符串,以使后续处理更易于管理

例如,下面的多索引:

# testing
def testing():
    testing_df = pd.read_csv("data/testing.csv", float_precision="high")
    testing_df = testing_df.set_index(["class", "table", "seat"]).sort_index()
    print("\n1: \n" + str(testing_df.to_string()))

    print("\n2 test: \n" + str(testing_df.index))
    occurrences = collections.Counter(testing_df.index)

    print("\n3: \n" + str(occurrences))
输出:

1: 
                    random_no
class   table seat           
Emerald 1     0         55.00
Ruby    0     0         33.67
              0         24.01
              1         87.00
Topaz   0     0         67.00

2 test: 
MultiIndex([('Emerald', 1, 0),
            (   'Ruby', 0, 0),
            (   'Ruby', 0, 0),
            (   'Ruby', 0, 1),
            (  'Topaz', 0, 0)],
           names=['class', 'table', 'seat'])

3: 
Counter({('Ruby', 0, 0): 2, ('Emerald', 1, 0): 1, ('Ruby', 0, 1): 1, ('Topaz', 0, 0): 1})
从3)中可以看出,它返回不同数据类型的元组组合作为dict键,这使得处理变得困难

我试着把它分开,或者把它变成字符串,这样处理起来就容易多了

尝试了以下错误:

x = "|".join(testing_df.index)
print(x)

 x = "|".join(testing_df.index)
TypeError: sequence item 0: expected str instance, tuple found
和下面的错误

x = "|".join(testing_df.index[0])
print(x)

 x = "|".join(testing_df.index[0])
 TypeError: sequence item 1: expected str instance, numpy.int64 found
基本上,其一:

  • 在计算集合之前,我将组合转换成字符串。计数器或
  • 将其转换为collections.Counter后,所有的键都是元组,并将这些键转换为字符串
  • 我能问一下我如何正确地做到这一点吗

    多谢各位