我有下表
我想使用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.pyx”,第108行,在pandas._libs.index.IndexEngine.get_loc文件中
“ pandas / _libs / index.pyx”,第132行
pandas._libs.index.IndexEngine.get_loc文件
“ pandas / _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 KeyError:'ColumnA'
在处理上述异常期间,发生了另一个异常:
回溯(最近一次通话):文件“ 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
index = MultiIndex.from_arrays([index,data [columns]])文件“ C:_data \ learn \ Miniconda \ lib \ site-packages \ pandas \ core \ frame.py”,
第2927行,在getitem中
indexer = 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.pyx”,第132行
pandas._libs.index.IndexEngine.get_loc文件
“ pandas / _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
最佳答案
这可以解决吗?
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")
关于python - 使用DataFrame Python以矩阵格式显示列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57750264/