Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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 - Fatal编程技术网

Python 从数据帧行生成存在和不存在矩阵

Python 从数据帧行生成存在和不存在矩阵,python,pandas,Python,Pandas,我有一个 df= Index 0 1 2 3 4 GCF_1 DUF137 F-type HP1 lyso nan GCF_2 DUF137 F-type HP2 lyso lytic 我正在寻找一个结果 df2 = DUF1 DUF2 F-type HP1 HP2 lyso lytic GCF_1 1 0 1 1 0

我有一个

df=

Index   0       1       2    3      4
GCF_1   DUF137  F-type  HP1  lyso   nan
GCF_2   DUF137  F-type  HP2  lyso   lytic
我正在寻找一个结果

df2 = 
        DUF1    DUF2    F-type  HP1  HP2    lyso    lytic
GCF_1   1       0       1       1    0      1       0
GCF_2   0       1       1       0    1      1       1
我试图通过制作列表和进行列表比较;然而,有没有办法从熊猫数据帧解决这个问题?此外,在R中似乎只有一个解决方案,但熊猫数据帧没有,尽管标题几乎与此相似

library(reshape2)
df2 <- melt(df, id.var = "V1")
with(df2, table(V1, value))
library(重塑2)
df2