【发布时间】:2021-07-09 16:12:54
【问题描述】:
想请教一些关于如何正确执行此操作的建议。我是 python 新手。
最初我想找出多索引组合的计数器/频率。我尝试了几种方法,例如循环、itertuples、iterrows 等,我意识到最快和最少开销是使用 collections.Counter
但是,它返回多索引索引组合的元组列表作为计数器字典键。元组的键使得后续处理变得困难。
因此,我正在研究如何将它们变成带有分隔符的字符串,以便之后的处理更易于管理。
例如下面这个多索引:
# 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 或之前将组合变成字符串
- 将其制成 collections.Counter 后,其中所有众多键都是元组并将这些键转换为字符串
请问我该如何正确执行此操作?
非常感谢!
【问题讨论】:
标签: python pandas dictionary collections multi-index