Package: pytabula
p     y     t    a     b    u    l   a
----- ----- ---- ----- ---- ---- --- -----
━━━━  ━━━━  ┏┓━  ━━━━  ┓━━  ━━━  ┓━  ━━━━
━━━━  ━━━━  ┛┗┓  ━━━━  ┃━━  ━━━  ┃━  ━━━━
┏━━┓  ┓━┏┓  ┓┏┛  ━━┓━  ┗━┓  ┓┏┓  ┃━  ━━┓━
┃┏┓┃  ┃━┃┃  ┃┃━  ━┓┃━  ┏┓┃  ┃┃┃  ┃━  ━┓┃━
┃┗┛┃  ┗━┛┃  ┃┗┓  ┗┛┗┓  ┗┛┃  ┗┛┃  ┗┓  ┗┛┗┓
┃┏━┛  ━┓┏┛  ┗━┛  ━━━┛  ━━┛  ━━┛  ━┛  ━━━┛
┃┃━━  ━┛┃━  ━━━  ━━━━  ━━━  ━━━  ━━  ━━━━
┗┛━━  ━━┛━  ━━━  ━━━━  ━━━  ━━━  ━━  ━━━━pytabulapytabulais aPythonlibrary for atabular databased oncollections.abc.Sequence.A table is a sequence of items organized in rows and columns, much like a spreadsheet.
Although it is missing from Python standard library, there are numerousalternative solutions.pytabula.Tableis designed to be minimal and intuitive subclass of theSequenceclass.Table, as such is a sequence ofrows, with each row being a sequence of individualitems.
Rows are indexed numerically and columns are named, allowing every item to be referenced by (row, column) pair.Installpytabulacan be easily pip installed.First create and/or activate virtual environment:$ python3.11 -m venv venv
$ source venv/bin/activate
(venv) $pip installpytabulainto virtual environment:(venv) $ python -m pip install pytabula
Collecting pytabula
  Using cached pytabula-0.2.0-py3-none-any.whl (5.9 kB)
Installing collected packages: pytabula
Successfully installed pytabula-0.2.0
(venv) $Check thatpytabulais importable:(venv) $ python -c "import pytabula as pt; print(pt.__pytabula__)"
p     y     t    a     b    u    l   a
----- ----- ---- ----- ---- ---- --- -----
━━━━  ━━━━  ┏┓━  ━━━━  ┓━━  ━━━  ┓━  ━━━━
━━━━  ━━━━  ┛┗┓  ━━━━  ┃━━  ━━━  ┃━  ━━━━
┏━━┓  ┓━┏┓  ┓┏┛  ━━┓━  ┗━┓  ┓┏┓  ┃━  ━━┓━
┃┏┓┃  ┃━┃┃  ┃┃━  ━┓┃━  ┏┓┃  ┃┃┃  ┃━  ━┓┃━
┃┗┛┃  ┗━┛┃  ┃┗┓  ┗┛┗┓  ┗┛┃  ┗┛┃  ┗┓  ┗┛┗┓
┃┏━┛  ━┓┏┛  ┗━┛  ━━━┛  ━━┛  ━━┛  ━┛  ━━━┛
┃┃━━  ━┛┃━  ━━━  ━━━━  ━━━  ━━━  ━━  ━━━━
┗┛━━  ━━┛━  ━━━  ━━━━  ━━━  ━━━  ━━  ━━━━

(venv) $Usepytabulaintroduces two classes, namelyTableandMutableTable, as subclasses ofSequenceandMutableSequence.
This implies that you can utilizeTableandMutableTablein a manner similar to how you would usetupleandlist, respectively. In this context, we'll exclusively showcase the enhancements made to this API.>>># import>>>importpytabulaastbl>>># initialization>>>t=tbl.Table([...('ozzy','dog',18),...('marry','cat',10),...],columns=('name','animal','age'))>>>print(t)nameanimalage-----------------ozzydog18marrycat10Table operationsCommon Table OperationsCheckcommon sequence operationsfor full overview.Containsshow/hide sequence apiSequence>>># row in table>>>('marry','cat',10)intTrue>>># item in table>>>any('dog'inrowforrowint)True>>># row not in table>>>('ozzy','cat',18)notintTrue>>># item not in table>>>any('bird'notinrowforrowint)TrueTable>>># row in table>>>('marry','cat',10)intTrue>>># item in table>>>'dog'intTrue>>># row not in table>>>('ozzy','cat',18)notintTrue>>># item not in table>>>'bird'notintTrueGet itemshow/hide sequence apiSequence>>>importenum>>>classCol(enum.IntEnum):...NAME=0...ANIMAL=1...AGE=2>>># row indexing>>>t[0]('ozzy','dog',18)>>># item indexing>>>t[1][Col.ANIMAL]'cat'>>># row slicing>>>t[:1]Table([('ozzy','dog',18)],columns=('name','animal','age'))>>># column slicing>>>t[0][:Col.AGE]('ozzy','dog')>>># rectangle>>>tbl.Table([r[:Col.AGE]forrint[:1]],columns=t.columns[:Col.AGE])Table([('ozzy','dog')],columns=('name','animal'))Table>>># import enum>>># class Col(enum.IntEnum):>>>#     NAME = 0>>>#     ANIMAL = 1>>>#     AGE = 2>>># row indexing>>>t[0]('ozzy','dog',18)>>># item indexing>>>t[1,'animal']'cat'>>># row slicing>>>t[:1]Table([('ozzy','dog',18)],columns=('name','animal','age'))>>># column slicing>>>t[0,:'age']('ozzy','dog')>>># rectangle>>>t[:1,:'age']Table([('ozzy','dog')],columns=('name','animal'))Index and countshow/hide sequence apiSequence>>># row index>>>t.index(('marry','cat',10))1>>># item index>>>row,col=next((i,Col(r.index(18)))fori,rinenumerate(t)if18inr)>>>row,col(0,<Col.AGE:2>)>>>t[row][col]18>>># row count>>>t.count(('ozzy','dog',18))1>>># item count>>>sum(r.count('cat')forrint)1Table>>># row index>>>t.index(('marry','cat',10))1>>># item index>>>row,col=t.index(18)>>>row,col(0,'age')>>>t[row,col]18>>># row count>>>t.count(('ozzy','dog',18))1>>># item count>>>t.count('cat')1Immutable Table typesCheckimmutable sequence typesfor full overview.Mutable Table operationsCheckmutable sequence typesfor full overview.Set itemshow/hide sequence apiSequence>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>># row update>>>mt[0]=('harry','mouse',2)>>>print(mt)nameanimalage-----------------harrymouse2marrycat10>>># item update>>>mt[0][Col.ANIMAL]='python'>>>print(mt)nameanimalage-----------------harrypython2marrycat10>>># slice update>>>forrow,valueinzip(mt,[('rabbit',3),('spider',1)]):...row[Col.ANIMAL:]=value>>>print(mt)nameanimalage-----------------harryrabbit3marryspider1Table>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>># row update>>>mt[0]=('harry','mouse',2)>>>print(mt)nameanimalage-----------------harrymouse2marrycat10>>># item update>>>mt[0,'animal']='python'>>>print(mt)nameanimalage-----------------harrypython2marrycat10>>># slice update>>>mt[:,'animal':]=tbl.Table([('rabbit',3),('spider',1)])>>>print(mt)nameanimalage-----------------harryrabbit3marryspider1Append columnshow/hide sequence apiSequence>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>>forrow,valueinzip(mt,('chicken','fish')):...row.append(value)>>>mt.columns.append('food')>>>print(mt)nameanimalagefood-------------------------ozzydog18chickenmarrycat10fishTable>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>>mt[:,'food']=('chicken','fish')>>>print(mt)nameanimalagefood-------------------------ozzydog18chickenmarrycat10fishInsert column(s)show/hide sequence apiSequence>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>>forrow,valueinzip(mt,('big','small')):...row[Col.AGE:Col.AGE]=(value,)>>>mt.columns[Col.AGE:Col.AGE]=('size',)>>>print(mt)nameanimalsizeage-----------------------ozzydogbig18marrycatsmall10Table>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>>mt[:,'age':'age']=tbl.Table([('big',),('small',)],columns=('size',))>>>print(mt)nameanimalsizeage-----------------------ozzydogbig18marrycatsmall10Delete itemshow/hide sequence apiSequence>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>># row deleting>>>delmt[1]>>>print(mt)nameanimalage----------------ozzydog18>>># column deleting>>>forrowinmt:...delrow[Col.AGE]>>>delmt.columns[Col.AGE]>>>print(mt)nameanimal------------ozzydogTable>>>mt=tbl.MutableTable(list(t),columns=t.columns)>>># row deleting>>>delmt[1]>>>print(mt)nameanimalage----------------ozzydog18>>># column deleting>>>delmt[:,'age']>>>print(mt)nameanimal------------ozzydog