簡體   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