Python系列,其中值是列表,获取另一个具有与每个项目列表对应的索引列表的系列

问题描述 投票:3回答:3

对不起,标题很难理解-不确定如何用短语表达。说我有一个看起来像这样的系列

s = pd.Series(index = ['a','b','c'], data = [['x','y','z'], ['y','z'], ['x','z']]). 

我想要这样的东西

{'x':['a','c'], 'y':['a','b'], 'z':['a','b','c']}

即我可以看到哪些键对应于一系列列表中的每个元素。有什么想法我如何尽可能有效地做到这一点?谢谢!

python pandas
3个回答
4
投票

让我们使用explode

s.explode().reset_index().groupby(0)['index'].agg(list).to_dict()
{'x': ['a', 'c'], 'y': ['a', 'b'], 'z': ['a', 'b', 'c']}

0
投票

还有第二种解决方案:

x = s.explode() 

pd.DataFrame({'X':x.index, 'Y':x.values}).groupby('Y')['X'].apply(list).to_dict()


# {'x': ['a', 'c'], 'y': ['a', 'b'], 'z': ['a', 'b', 'c']}

0
投票

使用默认字典速度的另一种解决方案:

from collections import defaultdict

d = defaultdict(list)
q = s.explode()
for k, v in q.items():
    d[v].append(k)

dict(d)

输出:

{'x': ['a', 'c'], 'y': ['a', 'b'], 'z': ['a', 'b', 'c']}

时间:

%timeits.explode()。reset_index()。groupby(0)['index']。agg(list).to_dict()每个循环3.94 ms±119 µs(平均±标准偏差,共运行7次,每个循环100个循环)

%% timeit d = defaultdict(list)方法每100毫秒300 µs±33.4 µs(平均±标准偏差,每7次运行,1000每个循环)

© www.soinside.com 2019 - 2024. All rights reserved.