Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧作为掩码_Python_Pandas_Dataframe - Fatal编程技术网

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']