Files
kman/mtable.py
2020-06-26 11:26:19 +08:00

129 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#########################################################
## @file : mtable.py
## @desc : pandas is to large,
## so i implement a simple table data structure
## by my self
## @create : 2020/06/25
## @author : Chengan
## @email : douboer@gmail.com
#########################################################
class mTable():
def __init__(self, data=[], index=[], columns=[]):
self._data = data
self._index = index
self._columns = columns
self._width = 10
def dataframe(self, data=[], index=[], columns=[]):
self._data = data
self._index = index
self._columns = columns
def get_iat(self, row: int, column: int):
""" just like pandas iat"""
return self._data[row][column] if len(self._data)>0 else None
def get_num_rows(self):
return len(self._data) if len(self._data)>0 else 0
def get_num_columns(self):
return len(self._columns) if len(self._columns)>0 else 0
def reverse(self):
self._data.reverse()
def set_data(self, data=[]):
""" get table content"""
self._data = data
def get_data(self):
""" get table content"""
return self._data
def get_index(self):
""" just like pandas index"""
return self._index
def get_columns(self):
""" just like pandas columns"""
return self._columns
def set_repr_width(self, width):
self._width = width
def __repr__(self):
""" print table """
w = self._width
s = ''.ljust(w-1,' ')
for c in self._columns: s += c.ljust(w,' ')
s += '\n'
len_index = len(self._index)
for i, r in enumerate(self._data):
s += self._index[i].ljust(w,' ') if i<len_index else str(i).ljust(w,' ')
for c in r:
s += str(c).ljust(w,' ')
s += '\n'
return s
def __next__(self):
# self.a就是当前要迭代的值
result = self.a
# 计算斐波那契数列的下一个值并将a变成原来的b将b变成下一个值
self.a, self.b = self.b, self.a + self.b
# 返回当前迭代的值
return result
def __iter__(self):
return self
if __name__=='__main__':
print('\n---------------------')
data = [['Ohiox','Ohio','Ohio','Nevada','Nevada'],
[2000,2001,2002,2001,2002],
[1.5,1.7,3.6,2.4,2.9],
[1.5,1.7,3.6,2.4,2.9],
]
mt = mTable(data,
index = ['one', 'two','three','four','five'],
columns = ['year','state','pop','debt','xx'])
print('== frame\n', mt)
print('== frame.iat[0, 0]\n', mt.get_iat(0, 0))
print('== frame.iat[1, 1]\n', mt.get_iat(1, 1))
print('== frame.iat[2, 2]\n', mt.get_iat(2, 2))
print('== frame.iat[2, 1]\n', mt.get_iat(2, 1))
print('== frame.shape[0]\n', mt.get_num_rows())
print('== frame.shape[1]\n', mt.get_num_columns())
print('== frame.columns\n', mt.get_columns())
print('== frame.index\n', mt.get_index())
mt = mTable(data,
columns = ['year','state','pop','debt','xx'])
print('== frame\n', mt)
print('== frame.iat[2, 1]\n', mt.get_iat(2, 1))
print('== frame.shape[0]\n', mt.get_num_rows())
print('== frame.shape[1]\n', mt.get_num_columns())
print('== frame.columns\n', mt.get_columns())
print('== frame.index\n', mt.get_index())
mt = mTable(data)
mt.set_repr_width(20)
print('== frame\n', mt)
print('== frame.iat[2, 1]\n', mt.get_iat(2, 1))
print('== frame.shape[0]\n', mt.get_num_rows())
print('== frame.shape[1]\n', mt.get_num_columns())
print('== frame.columns\n', mt.get_columns())
print('== frame.index\n', mt.get_index())
mt = mTable()
print('== frame\n', mt)
print('== frame.iat[2, 1]\n', mt.get_iat(2, 1))
print('== frame.shape[0]\n', mt.get_num_rows())
print('== frame.shape[1]\n', mt.get_num_columns())
print('== frame.columns\n', mt.get_columns())
print('== frame.index\n', mt.get_index())