本文整理汇总了Python中pyvttbl.DataFrame.insert方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.insert方法的具体用法?Python DataFrame.insert怎么用?Python DataFrame.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyvttbl.DataFrame
的用法示例。
在下文中一共展示了DataFrame.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_csv_pivot
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def export_csv_pivot(request, entidad=1, ano=str(date.today().year)):
consumos = Consumo.objects.filter(entidad__pk=entidad, ano=ano)
from collections import namedtuple
LineaDetalle = namedtuple('LineaDetalle',[u'Año', "Mes", 'Local_o_Vehiculo', "Consumo", "Valor"])
df = DataFrame()
for c in consumos:
if c.content_type.id == 16:
denominacion = Local.objects.get(pk=c.object_id).denominacion
else:
denominacion = Vehiculo.objects.get(pk=c.object_id).denominacion
df.insert(LineaDetalle(c.ano, c.mes, denominacion.encode("utf-8"), c.medida.denominacion.encode("utf-8"), c.valor)._asdict())
pt = df.pivot("Valor", ['Local_o_Vehiculo','Consumo'], ['Mes'])
# get the response object, this can be used as a stream.
response = HttpResponse(mimetype='text/csv')
# force download.
response['Content-Disposition'] = 'attachment;filename=export.csv'
response.write(pt)
return response
示例2: test1
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test1(self):
df=DataFrame()
with self.assertRaises(Exception) as cm:
df.insert([1,2,3,4])
self.assertEqual(str(cm.exception),
'row must be mappable type')
示例3: test4
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test4(self):
df=DataFrame()
df.insert([('GROUP','AA'),('VAL',1)])
with self.assertRaises(Exception) as cm:
df.validate(lambda x: x in ['AA', 'AB', 'LAB'])
self.assertEqual(str(cm.exception),
'criteria must be mappable type')
示例4: test3
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test3(self):
df=DataFrame()
df.insert({'A':1, 'B':2})
with self.assertRaises(Exception) as cm:
df.insert({'A':1, 'B':2, 'C':3})
self.assertEqual(str(cm.exception),
'row must have the same keys as the table')
示例5: test3
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test3(self):
tupa = ('a1','a2','a3')
tupb = ('a1','b2','b3')
df=DataFrame()
df.insert([(('a1','a2','a3'),34), (('a1','b2','b3'),1)])
df.insert([(('a1','a2','a3'),34), (('a1','b2','b3'),2)])
df.insert([(('a1','a2','a3'),42), (('a1','b2','b3'),3)])
namea,nameb = df.keys()
self.assertEqual(namea, tupa)
self.assertEqual(nameb, tupb)
示例6: test0
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test0(self):
df=DataFrame()
conditionsDict=DictSet({'A':[10,20,40,80],
'B':[100,800],
'rep':range(10)})
for A,B,rep in conditionsDict.unique_combinations():
df.insert({'A':A, 'B':B,'rep':rep})
for d,r in zip(df['A'],_rep_generator([10,20,40,80],4,20)):
self.assertAlmostEqual(d,r)
for d,r in zip(df['B'],_rep_generator([100,800],8,10)):
self.assertAlmostEqual(d,r)
for d,r in zip(df['rep'],_rep_generator(range(10),8,1)):
self.assertAlmostEqual(d,r)
示例7: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test2(self):
R={'A': [-8.0, -7.0, -3.0, -2.0, -1.0, 1.0, 2.0, 3.0, 4.0, 9.0, -10.0, -9.0, -6.0, -5.0, -4.0, 0.0, 5.0, 6.0, 7.0, 8.0],
'B': [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]}
a=[4, 8, 1, 5, -7, -5, 9, 7, -8, -10, -1, -4, 3, 0., -2, 6, 2, -9, -3, -6]
b=[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
df=DataFrame()
for A,B in zip(a,b):
df.insert({'A':A, 'B':B})
df.sort(['B','A'])
for d,r in zip(df['A'],R['A']):
self.assertAlmostEqual(d,r)
for d,r in zip(df['B'],R['B']):
self.assertAlmostEqual(d,r)
示例8: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test2(self):
"""method='valid', aggregate=count, invalid col"""
R = """\
count(id)
member Name=name1, Name=name1, Name=name2, Total
Year=2010 Year=2011 Year=2011
========================================================
N 0 1 0 1
Y 1 0 1 2
========================================================
Total 1 1 1 3 """
df = DataFrame()
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y'})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N'})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y'})
my_pivot = df.pivot('id',rows = ['member'], cols = ['Name','Year'], aggregate='count')
self.assertEqual(R,str(my_pivot))
示例9: test1
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test1(self):
"""method='valid', aggregate=count, invalid row"""
R = """\
count(id)
Name Year member=N member=Y Total
==========================================
name1 2010 0 1 1
name1 2011 1 0 1
name2 2011 0 1 1
==========================================
Total 1 2 3 """
df = DataFrame()
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y'})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N'})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y'})
my_pivot = df.pivot('id',rows = ['Name','Year'], cols = ['member'], aggregate='count')
self.assertEqual(R,str(my_pivot))
示例10: test4
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test4(self):
"""method='full', aggregate=tolist, invalid col"""
R = """\
tolist(id)
member Name=name1, Name=name1, Name=name2, Name=name2,
Year=2010 Year=2011 Year=2010 Year=2011
==================================================================
N [None, None] [1.0, 1.0] [None, None] [None, None]
Y [0.0, 0.0] [None, None] [None, None] [2.0, 2.0] """
df = DataFrame()
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y','rep':1})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N','rep':1})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y','rep':1})
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y','rep':2})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N','rep':2})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y','rep':2})
my_pivot = df.pivot('id',rows = ['member'], cols = ['Name','Year'],
aggregate='tolist', method='full')
## print(my_pivot)
self.assertEqual(R,str(my_pivot))
示例11: test3
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test3(self):
"""method='full', aggregate=tolist, invalid row"""
R = """\
tolist(id)
Name Year member=N member=Y
==========================================
name1 2010 [None, None] [0.0, 0.0]
name1 2011 [1.0, 1.0] [None, None]
name2 2010 [None, None] [None, None]
name2 2011 [None, None] [2.0, 2.0] """
df = DataFrame()
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y','rep':1})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N','rep':1})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y','rep':1})
df.insert({'id':0,'Name':'name1','Year':2010,'member':'Y','rep':2})
df.insert({'id':1,'Name':'name1','Year':2011,'member':'N','rep':2})
df.insert({'id':2,'Name':'name2','Year':2011,'member':'Y','rep':2})
my_pivot = df.pivot('id',rows = ['Name','Year'], cols = ['member'],
aggregate='tolist', method='full')
## print(my_pivot)
self.assertEqual(R,str(my_pivot))
示例12: apply_method
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def apply_method(self, r, **attr):
db = current.db
response = current.response
if not PYVTTBL:
r.error(501, "Function not available on this server")
if r.interactive:
# Get rows, cols, fact and aggregate from URL
if "row" in r.get_vars:
rowname = r.get_vars["row"]
self.rows = [rowname]
else:
r.error(400, "Missing row parameter")
if "col" in r.get_vars:
colname = r.get_vars["col"]
self.cols = [colname]
else:
r.error(400, "Missing col parameter")
if "fact" in r.get_vars:
self.fact = r.get_vars["fact"]
else:
r.error(400, "Missing fact parameter")
if "aggregate" in r.get_vars:
self.aggregate = r.get_vars["aggregate"]
else:
self.aggregate = "group_concat"
# Get the fields
fields = []
if self.rows:
fields.extend([f for f in self.rows if f not in fields])
if self.cols:
fields.extend([f for f in self.cols if f not in fields])
if self.fact and self.fact not in fields:
fields.append(self.fact)
list_fields = list(fields)
lfields, join = self.get_list_fields(self.resource.table, list_fields)
lfields = Storage([(f.fieldname, f) for f in lfields])
# Get the items
items = self.sqltable(list_fields, as_list=True)
# Map the items into a data frame
df = DataFrame()
for row in items:
frow = Storage()
for field in fields:
lfield = lfields[field]
tname = lfield.tname
fname = lfield.fname
if fname in row:
value = row[fname]
elif tname in row and fname in row[tname]:
value = row[tname][fname]
else:
value = None
frow[field] = value
df.insert(frow)
# Pivot table
try:
pt = df.pivot(self.fact, self.rows, self.cols, aggregate=self.aggregate)
except:
r.error(400, "Could not generate contingency table", next=r.url(method=""))
items = S3ContingencyTable(pt,
rows=self.rows,
cols=self.cols,
fact=self.fact,
lfields=lfields,
_id="list",
_class="dataTable display")
output = dict(items=items)
else:
r.error(501, self.manager.ERROR.BAD_FORMAT)
response.s3.no_sspag = True
response.view = self._view(r, "list.html")
return output
示例13: test4
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def test4(self):
df=DataFrame()
df.insert([('A',1.23), ('B',2), ('C','A')])
self.assertEqual(df.types(), ['real', 'integer', 'text'])
示例14: OrderedDict
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
# figure out pid and independent variable conditions
pid = daq.etc['pid']
trial = epoch / 10
scenario = daq.etc['scen_order'][trial]
section = epoch % 10
# pack pid and IV conditions into OrderedDict
row = OrderedDict([('pid', pid),
('trial', trial),
('scenario', scenario),
('section', section)])
for (dv, indx) in dvs:
vec = daq[dv][indx,fslice].flatten()
row['%s_mean'%dv] = np.mean(vec)
row['%s_min'%dv] = np.min(vec)
row['%s_max'%dv] = np.max(vec)
row['%s_range'%dv] = row['%s_max'%dv] - row['%s_min'%dv]
row['%s_amean'%dv] = np.mean(np.abs(vec))
row['%s_sd'%dv] = np.std(vec)
row['%s_rms'%dv] = np.linalg.norm(vec)/np.sqrt(len(vec))
# insert the row into the dataframe
df.insert(row)
df.write(fname='collaborated_ts.csv')
print('\nDone.\n\ncollaborating timeseries measureas took %.1f s'%(time.time()-t0))
示例15: pivot
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import insert [as 别名]
def pivot(self, r, **attr):
"""
Method to generate contingency tables
@param r: the S3Request instance
@param attr: controller attributes for the request
@todo: warn if dimension/fact doesn't belong to table
@todo: support component fields
@todo: support automatic generation of dimension classes
@todo: optimize inner loops
@todo: make facts inline-expandable
"""
db = current.db
T = current.T
response = current.response
resource = self.resource
table = resource.table
pkey = table._id.name
# Get the fields
fields = []
if self.rows:
fields.extend([f for f in self.rows if f not in fields])
if self.cols:
fields.extend([f for f in self.cols if f not in fields])
if self.fact and self.fact not in fields:
fields.append(self.fact)
list_fields = list(fields)
lfields, join = self.get_list_fields(table, list_fields)
lfields = Storage([(f.fieldname, f) for f in lfields])
# Get the items
items = self.sqltable(list_fields,
as_list=True, start=None, limit=None)
count = None
if items:
# Generate the data frame
aggregate = self.aggregate == "list" and "group_concat" or \
self.aggregate
from pyvttbl import DataFrame
df = DataFrame()
fmap = Storage()
item_list = []
for row in items:
try:
frow = self.__extract(row, fields, resource, lfields, fmap)
except KeyError:
e = sys.exc_info()[1]
if hasattr(e, "message"):
e = e.message
r.error(400, e, next=r.url(vars=[]))
frow = self.__expand(frow)
for item in frow:
if aggregate in ("group_concat", "count"):
i = Storage(item)
if self.fact != pkey:
del i[pkey]
# De-duplicate
if i not in item_list:
item_list.append(i)
df.insert(aggregate == "count" and i or item)
else:
df.insert(item)
# In group_concat, always group primary keys, and then use
# fmap to link individual fact values to their corresponding
# records
if aggregate == "group_concat" and fmap:
fact = pkey
else:
# Otherwise group the fact value, and delete fmap
fact = self.fact
fmap = None
# Pivoting
try:
pt = df.pivot(fact, self.rows, self.cols,
aggregate=aggregate)
except:
r.error(400, "Could not generate contingency table",
next=r.url(vars=[]))
if aggregate == "count" or pt.grand_tot is None:
count = len(item_list)
# Generate output table
items = S3ContingencyTable(r,
self.resource,
pt,
rows=self.rows,
cols=self.cols,
fact=self.fact,
lfields=lfields,
fmap=fmap,
count=count,
_id="list",
#.........这里部分代码省略.........