Python 将数据帧作为掩码
我有两个数据帧,如下所示: 发生次数Python 将数据帧作为掩码,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧,如下所示: 发生次数 doc 0 1 2 ... 1809(=n) 0 0 0 1 ... 1 1 0 0 1 ... 0 2 0 0 1 ... 0 .. .. .. .. ... . m ......................... 0 字典 id term 0 f
doc 0 1 2 ... 1809(=n)
0 0 0 1 ... 1
1 0 0 1 ... 0
2 0 0 1 ... 0
.. .. .. .. ... .
m ......................... 0
字典
id term
0 foo
1 bar
2 lorem
.. ..
n ipsum
我试图做的是,为每一行“引用”检索具有“1”作为单元格值的术语(通过id,即第一个数据帧中的列标题)。
在我的示例中,考虑到第一行事件,我会有:['lorem','ipsum']
谢谢这里有一个关于
np.where
occurrences = pd.DataFrame([[0,0,1,1],[0,1,0,1], [1,0,1,0]])
dictionary=pd.DataFrame({'term':['foo','bar', 'lorem', 'ipsum']})
idx = np.where(occurrences)
(pd.Series(dictionary.values[idx[1]].ravel())
.groupby(idx[0]).agg(list)
)
输出:
0 [lorem, ipsum]
1 [bar, ipsum]
2 [foo, lorem]
dtype: object
经过一些尝试后,我让它以这种方式工作(也许不是那么酷…) 最终输出为:
['scheduling', 'distributed', 'deadline', .... , 'rate monotonic scheduling algorithm']
到目前为止你尝试了什么,结果如何?
['scheduling', 'distributed', 'deadline', .... , 'rate monotonic scheduling algorithm']