繁体   English   中英

根据列列表中的值对Pandas Dataframe进行切片

[英]Slicing Pandas Dataframe based on a value present in a column which is a list of lists

我有一个带有一百万行(id)的熊猫数据框,其中一列作为列表列表。 例如

df = pd.DataFrame({'id':[1,2,3,4],'token_list':[['a','b','c'],['c','d'], ['a','e','f'],['c','f']]})

我想创建一个包含所有唯一标记的字典-'a','b','c','e','f'(我已经作为单独的列表)作为键以及每个键的所有ID与..相联系。 例如,{'a':[1,3],'b':[1],'c':[1、2,4] ..}等等。

我的问题是有12000个这样的令牌,我不想使用循环来遍历第一帧的每一行。 并在似乎不起作用。

使用np.repeatnumpy.concatenate为第一平整,然后groupbylist和最后to_dict

a = np.repeat(df['id'], df['token_list'].str.len())
b = np.concatenate(df['token_list'].values)

d = a.groupby(b).apply(list).to_dict()
print (d)

{'c': [1, 2, 4], 'a': [1, 3], 'b': [1], 'd': [2], 'e': [3], 'f': [3, 4]}

详情:

print (a)
0    1
0    1
0    1
1    2
1    2
2    3
2    3
2    3
3    4
3    4
Name: id, dtype: int64

print (b)
['a' 'b' 'c' 'c' 'd' 'a' 'e' 'f' 'c' 'f']
df.set_index('id')['token_list'].\
    apply(pd.Series).stack().reset_index(name='V').\
       groupby('V')['id'].apply(list).to_dict()
Out[359]: {'a': [1, 3], 'b': [1], 'c': [1, 2, 4], 'd': [2], 'e': [3], 'f': [3, 4]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM