繁体   English   中英

使用DataFrame Python以矩阵格式显示列

[英]Display columns in matrix format using dataframe python

我有下表

在此处输入图片说明

我想使用python将int转换为矩阵,如下所示:

在此处输入图片说明

我可以从哪里开始获得一些指导吗? 我用熊猫读取了两个数据框,并将它们合并以创建我显示的初始表(一个有两列)。

我在下面使用的代码如下:

import pandas as pd
from pyexcelerate import Workbook
import numpy as np
import time
start = time.process_time()
excel_file = 'Test.xlsx'
df = pd.read_excel(excel_file, sheet_name=0, index_col=0)
print(df.columns)
print(df.index)

newdf= (df.pivot(index='ColumnB',columns='ColumnA', values='ColumnB'))
myNewDF = newdf.transform(lambda x: np.where(x.isnull(), '', 'yes'))
aftercalc = time.process_time()
print(aftercalc - start)

myNewDF.to_excel("1.xlsx")
print(time.process_time() - aftercalc)

印刷品的输出是:

Index(['ColumnB'],dtype ='object')Index(['TypeA','TypeA','TypeA','TypeA','TypeA','TypeB','TypeB','TypeC',' TypeC','TypeC','TypeD'],dtype ='object',name ='ColumnA')

我在运行此程序时遇到的错误是:

追溯(最近一次通话):文件“ C:_data \\ learn \\ Miniconda \\ lib \\ site-packages \\ pandas \\ core \\ indexes \\ base.py”,行2657,在get_loc返回self._engine.get_loc(key)文件pandas._libs.index.IndexEngine.get_loc文件中的第108行中的“ pandas / _libs / index.pyx”,pandas._libs.index.IndexEngine.get_loc文件“ pandas”中第132行中的“ pandas / _libs / index.pyx” /_libs/hashtable_class_helper.pxi”,第1601行,在pandas._libs.hashtable.PyObjectHashTable.get_item中,文件“ pandas / _libs / hashtable_class_helper.pxi”,第1608行,在pandas._libs.hashtable.PyObjectHashTable:get_item

在处理上述异常期间,发生了另一个异常:

追溯(最近一次通话最近):文件“ test.py”,第10行,在newdf =中(df.pivot(index ='ColumnB',columns ='ColumnA',values ='ColumnB')))文件“ C:_data \\ learn \\ Miniconda \\ lib \\ site-packages \\ pandas \\ core \\ frame.py“,第5628行,在数据透视表返回数据透视表(自身,索引=索引,列=列,值=值)中,文件“ C:_data \\ learn \\ Miniconda \\ lib \\ site-packages \\ pandas \\ core \\ reshape \\ pivot.py“,第379行,位于枢轴索引= MultiIndex.from_arrays([index,data [columns]])文件” C:_data \\ learn \\ Miniconda \\ lib \\ site-packages \\ pandas \\ core \\ frame.py“,第2927行,位于getitem索引器= self.columns.get_loc(key)文件“ C:_data \\ learn \\ Miniconda \\ lib \\ site-packages \\ pandas \\ core \\ indexes \\ base.py”,第2659行,在get_loc中返回self._engine.get_loc(self._maybe_cast_indexer(key))文件“ pandas / _libs / index.pyx”,第108行,在pandas._libs.index.IndexEngine.get_loc文件中pandas._libs.index.IndexEngine.get_loc中的第132行“ pandas / _libs / index.pyx”,pandas._libs.hashtable.PyObjectHashTable.g中的第1601行“ pandas / _libs / hashtable_class_helper.pxi” et_item文件“ pandas / _libs / hashtable_class_helper.pxi”,行1608,在pandas._libs.hashtable.PyObjectHashTable.get_item中

这可以解决吗?

newdf= (df.pivot(index='ColumnB',columns='ColumnA', values='ColumnB'))

newdf
Out[28]: 
ColumnA TypeA TypeB TypeC TypeD
ColumnB                        
A           A     A   NaN     A
B           B   NaN     B   NaN
C           C   NaN     C   NaN
D           D   NaN   NaN   NaN
E           E   NaN   NaN   NaN
F         NaN     F   NaN   NaN
Z         NaN   NaN     Z   NaN

newdf.transform(lambda x: np.where(x.isnull(), '', 'yes'))
Out[29]: 
ColumnA TypeA TypeB TypeC TypeD
ColumnB                        
A         yes   yes         yes
B         yes         yes      
C         yes         yes      
D         yes                  
E         yes                  
F               yes            
Z                     yes      

修改后的代码

import pandas as pd
#from pyexcelerate import Workbook
import time
import numpy as np
start = time.process_time()
excel_file = 'C:\\Users\\ss\\Desktop\\check.xlsx'
df = pd.read_excel(excel_file, sheet_name=0, index_col=0)
print(df.columns)
print(df.index)

newdf= (df.pivot(index='ColumnB',columns='ColumnA', values='ColumnB'))
myNewDF = newdf.transform(lambda x: np.where(x.isnull(), '', 'yes'))
aftercalc = time.process_time()
print(aftercalc - start)

myNewDF.to_excel("C:\\Users\\ss\\Desktop\\output.xlsx")

我们可以做的

pd.crosstab(df.ColumnA,df.ColumnB).astype(bool)

暂无
暂无

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

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