本文整理汇总了Python中pyvttbl.DataFrame.pivot方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.pivot方法的具体用法?Python DataFrame.pivot怎么用?Python DataFrame.pivot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyvttbl.DataFrame
的用法示例。
在下文中一共展示了DataFrame.pivot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test_pivot_2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
class Test_pivot_2(unittest.TestCase):
def setUp(self):
D={
'SUBJECT':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],
'AGE':'old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,old,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young,young'.split(','),
'CONDITION':'counting,counting,counting,counting,counting,counting,counting,counting,counting,counting,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,intention,intention,intention,intention,intention,intention,intention,intention,intention,intention,counting,counting,counting,counting,counting,counting,counting,counting,counting,counting,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,rhyming,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,adjective,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,imagery,intention,intention,intention,intention,intention,intention,intention,intention,intention,intention'.split(','),
'WORDS':[9,8,6,8,10,4,6,5,7,7,7,9,6,6,6,11,6,3,8,7,11,13,8,6,14,11,13,13,10,11,12,11,16,11,9,23,12,10,19,11,10,19,14,5,10,11,14,15,11,11,8,6,4,6,7,6,5,7,9,7,10,7,8,10,4,7,10,6,7,7,14,11,18,14,13,22,17,16,12,11,20,16,16,15,18,16,20,22,14,19,21,19,17,15,22,16,22,22,18,21],
}
self.df=DataFrame()
self.df.read_tbl('data/words~ageXcondition.csv')
def test12(self):
R="""\
avg(WORDS)
CONDITION AGE Value
==========================
adjective old 11
adjective young 14.800
counting old 7
counting young 6.500
imagery old 13.400
imagery young 17.600
intention old 12
intention young 19.300
rhyming old 6.900
rhyming young 7.600
==========================
Total 11.610 """
D = self.df.pivot('WORDS', rows=['CONDITION','AGE'])
list(D)
# verify the values in the table
self.failUnlessEqual(str(D),R)
def test13(self):
R="""\
avg(WORDS)
CONDITION=adjective, CONDITION=adjective, CONDITION=counting, CONDITION=counting, CONDITION=imagery, CONDITION=imagery, CONDITION=intention, CONDITION=intention, CONDITION=rhyming, CONDITION=rhyming, Total
AGE=old AGE=young AGE=old AGE=young AGE=old AGE=young AGE=old AGE=young AGE=old AGE=young
==================================================================================================================================================================================================================================
11 14.800 7 6.500 13.400 17.600 12 19.300 6.900 7.600 11.610 """
D = self.df.pivot('WORDS', cols=['CONDITION','AGE'])
# verify the values in the table
self.failUnlessEqual(str(D),R)
示例2: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test2(self):
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['MODEL','TIMEOFDAY'],['COURSE'],where=['SUBJECT != 1'])
self.assertEqual(repr(eval(repr(pt))), repr(pt))
示例3: test4
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [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))
示例4: test0
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test0(self):
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'])
self.assertAlmostEqual(3.22222222222,pt[1,0],5)
示例5: export_csv_pivot
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [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
示例6: test3
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [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))
示例7: long2wide
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def long2wide(in_fname, id, dvs, between=[], within=[],
covariates=[], out_fname=None, nested=True):
# load in_fname into a PyvtTbl object
print('reading "%s"...'%in_fname)
cls = DataFrame()
cls.read_tbl(in_fname)
# loop through DVs and append within columns
d = [sorted(set(cls[id]))]
header = [id] + covariates + between
for col in covariates+between:
z = cls.pivot(col, cols=[id], aggregate='arbitrary')
d.extend(list(z))
# start controls whether nested factors are examined
if nested : start = 1
else : start = len(within)
for i, dv in enumerate(dvs):
print('\ncollaborating %s'%dv)
for j in _xrange(start, len(within)+1):
for factors in _xunique_combinations(within, j):
print(' pivoting', factors, '...')
z = cls.pivot(dv, rows=factors, cols=[id],
aggregate='avg')
d.extend(list(z))
# process headers
for names in z.rnames:
h = '_'.join(('%s.%s'%(f, str(c)) for (f,c) in names))
header.append('%s__%s'%(dv, h))
# Now we can write the data
if out_fname == None:
out_fname = 'wide_data.csv'
with open(out_fname,'wb') as f:
wtr = csv.writer(f)
wtr.writerow([n.upper() for n in header])
wtr.writerows(zip(*d)) # transpose and write
示例8: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test2(self):
R =[7.16666666667, 6.5, 4.0, 3.22222222222, 2.88888888889, 1.55555555556]
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', cols=['TIMEOFDAY','COURSE'])
pt_flat = pt.flatten()
for r,d in zip(R,pt_flat):
self.assertAlmostEqual(r,d)
示例9: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test2(self):
R =[7.16666666667, 6.5, 4.0,
3.22222222222, 2.88888888889, 1.55555555556]
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['TIMEOFDAY','COURSE'])
for r,L in zip(R,pt.flat):
self.assertEqual('%.5f'%r, '%.5f'%L)
示例10: test7
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test7(self):
R ="""\
avg(ERROR)
TIMEOFDAY COURSE=C1 COURSE=C2
=================================
T1 7.167 6.500 """
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'])
self.assertEqual(R, str(pt[0,:2]))
示例11: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test2(self):
R = """\
GROUP=AA, GROUP=AA, GROUP=AB, GROUP=AB, GROUP=LAB, GROUP=LAB,
AGE=old AGE=young AGE=old AGE=young AGE=old AGE=young
=======================================================================
22.188 9.813 29.188 10.041 34.141 11.875 """
df = DataFrame()
df.read_tbl('data/suppression~subjectXgroupXageXcycleXphase.csv')
pt = df.pivot('SUPPRESSION',
cols=['GROUP', 'AGE'])
df2 = pt.to_dataframe()
self.assertEqual(str(df2),R)
示例12: test0
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test0(self):
R = """\
TIME CONDITION=A CONDITION=B
=================================
day 1.864 1.933
night 1.622 1.731 """
df = DataFrame()
df.read_tbl('data/example.csv')
df['LOG10_X']=[math.log10(x) for x in df['X']]
pt = df.pivot('LOG10_X', ['TIME'], ['CONDITION'])
df2 = pt.to_dataframe()
self.assertEqual(str(df2),R)
示例13: test1
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test1(self):
R ="""\
N/A(ERROR)
TIMEOFDAY COURSE=C1 COURSE=C2 COURSE=C3 Total
======================================================
T1 51.361 42.250 16 31.574
T2 10.383 8.346 2.420 6.531
======================================================
Total 23.040 18.778 7.716 15.178 """
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
sums = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'],aggregate='sum')
counts = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'],aggregate='count')
aves = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'],aggregate='avg')
calc_aves = sums/counts.astype(np.float64)
## print('\n'.join(str(aves).split('\n')[1:]))
## print('\n'.join(str(calc_aves).split('\n')[1:]))
self.assertEqual('\n'.join(str(aves).split('\n')[1:]),
'\n'.join(str(calc_aves).split('\n')[1:]))
示例14: test4
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test4(self):
R ="""\
avg(ERROR)
TIMEOFDAY COURSE=C1 COURSE=C2 COURSE=C3
=============================================
T2 3.222 2.889 1.556 """
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'])
self.assertEqual(R, str(pt[-1:]))
示例15: test2
# 需要导入模块: from pyvttbl import DataFrame [as 别名]
# 或者: from pyvttbl.DataFrame import pivot [as 别名]
def test2(self):
# __add__ ndarray
R ="""\
N/A(ERROR)
TIMEOFDAY COURSE=C1 COURSE=C2 COURSE=C3
=============================================
T1 12.167 11.500 9
T2 8.222 7.889 6.556 """
df=DataFrame()
df.read_tbl('data/error~subjectXtimeofdayXcourseXmodel_MISSING.csv')
pt = df.pivot('ERROR', ['TIMEOFDAY'],['COURSE'])
pt2=pt+np.array([[5,5,5], [5,5,5]])
self.assertEqual(str(pt2),R)