Wiki

Clone wiki

Tables / Home

Tables

Tables is a package/toolkit to manipulate structured data.

To Do To Think

Some news

I hope to continue the development in spare time. It was going pretty slow lately. Now some major changes should be done.

The Idea

The main idea is to provide tool to manipulate structured data in tables(like rdb) in memory. Data is collected into Tables. Table is a wrap agaist the list of dictionaries(for now. Inner data structure may be changed). The main goal is to provide methods to manipulate rows and columns of table. Some parts(e.g. pivoting) are inspired by dark package. NB! This module is not suitable to manipulate LARGE amounts of data.

Examples

from tables import Table

data = [
	{"id": 1, "name": "n1", "age": 22},
	{"id": 2, "name": "n2", "age": 23},
	{"id": 3, "name": "n3", "age": 21},
	{"id": 4, "name": "n4", "age": 22},
	{"id": 5, "name": "n5", "age": 23},
	{"id": 6, "name": "n6", "age": 25},
	{"id": 7, "name": "n7", "age": 22},
	{"id": 8, "name": "n8", "age": 21},
	]


t = Table(data)
print "t\n", t
>>>[{'age': 22, 'id': 1, 'name': 'n1'}, {'age': 23, 'id': 2, 'name': 'n2'}, 
>>>{'age': 21, 'id': 3, 'name': 'n3'}, {'age': 22, 'id': 4, 'name': 'n4'},
>>>{'age': 23, 'id': 5, 'name': 'n5'}, {'age': 25, 'id': 6, 'name': 'n6'}, 
>>>{'age': 22, 'id': 7, 'name': 'n7'}, {'age': 21, 'id': 8, 'name': 'n8'}]


#filtering
t2 = t.where(age=22)
print "t2\n", t2
>>> [{'age': 22, 'id': 1, 'name': 'n1'}, {'age': 22, 'id': 4, 'name': 'n4'}, {'age': 22, 'id': 7, 'name': 'n7'}]

#advanced filtering
def my_filter(x):
    if x["age"]>=20: return True
    else: return False
t21 = t.where(my_filter)

#lambda way
t22 = t.where(lambda x: x["age"]>=22)

#filter combinations
#you can use several filtering functions in a row
t23 = t.where(lambda x: x["age"]>=22 , lambda x: x["age"]<24 )

#beautiful filters
t24 = t.where(t["age"]>=22)


#making new table with limited columns
t3 = t("id","age")
t3
>>>[{'age': 22, 'id': 1}, {'age': 23, 'id': 2}, {'age': 21, 'id': 3}, {'age': 22, 'id': 4},
>>> {'age': 23, 'id': 5}, {'age': 25, 'id': 6}, {'age': 22, 'id': 7}, {'age': 21, 'id': 8}]


#adding new column
t["over forty"] = lambda x: x['age']>=40
#TODO: t["over forty"] = t["age"]>=40
#basic operations with columns
t["x+y"] = t["x"]+t["y"]
t["xyz"] = (t[x]+123)/t[y]
#simple arithmethic opearations are avaliable:
# + - / *

#Using aggregator as a new column generator
t["sum"] = Sum("key")
#will sum all values in row with meta-key "key"
#usefull after pivoting
#TODO: column value as a fuction


#Pivoting
from tables import aggregators
t4 = t.reshape(["age"],[], aggregators.Count("id",name="units") )
t4
[{'age': 25, ' units': 1}, {'age': 21, ' units': 2}, {'age': 22, ' units': 3}, {'age': 23, ' units': 2}]

#adding filter on an aggregator
t41 = t.reshape(["age"],[], aggregators.Count("id",name="units").filter(lambda x: x["id"]!=1) )


#Rendering

Updated