为了将列表转换为二维透视表,之前自己写过代码,今天看到pandas直接有这个方法,感觉简单顺手多了,故重写了社会特征属性的人口矩阵和距离矩阵分离的代码,首先介绍一下pivot_table:
pandas.tools.pivot.pivot_table(data, values=None, index=None, columns=None, aggfunc=‘mean‘,fill_value=None, margins=False, dropna=True)
value为显示的值,index为行,columns为列,如下面这个
df = pd.DataFrame({‘A‘ : [‘one‘, ‘one‘, ‘two‘, ‘three‘] * 3, ‘B‘ : [‘A‘, ‘B‘, ‘C‘] * 4,‘C‘ : [‘foo‘, ‘foo‘, ‘foo‘, ‘bar‘, ‘bar‘, ‘bar‘] * 2, ‘D‘ : np.random.randn(12),‘E‘ : np.random.randn(12)})
pd.pivot_table(df, values=‘D‘, index=[‘A‘, ‘B‘], columns=[‘C‘])
这样就是df表中以D作为数值域,A,B为行,C为列的数据视图。
下面是过剩通勤的社会特征分类的修正代码,换成pivot_table就简单了很多:
# authors = Kanonpy # coding=UTF-8 import pandas as pd import numpy as np import os from scipy.optimize import linprog distance = pd.read_excel(‘Distance.xlsx‘) df = pd.read_excel(‘chuli.xls‘) def commuteCalcu(pop,dist,name): #保证pop和dist行列数值相等 intdistcolumns = {d:int(float(d)) for d in dist.columns} intdistindex = {d:int(float(d)) for d in dist.index} unicodepopcolumns = {d:unicode(d) for d in pop.columns} unicodepopindex = {d:unicode(d) for d in pop.index} for d in dist.columns: if d not in pop.columns: dist = dist.drop(d,axis=1) #print ‘the col %s in distance was del‘%(str(d)) for i in dist.index: if i not in pop.index: dist = dist.drop(i,axis=0) #print ‘the col %s in distance was del ‘%(str(i)) for d in pop.columns: if d not in dist.columns: pop = pop.drop(d,axis=1) #print ‘the col %s in distance was del ‘%(str(i)) for i in pop.index: if i not in dist.index: pop = pop.drop(i,axis=0) #print ‘the col %s in distance was del ‘%(str(i)) if not os.path.exists(u‘%s‘%(name)): os.mkdir(u‘%s‘%(name)) print u‘creat %s_%s file‘%(col,i) pop.to_excel(u‘%s/Population.xlsx‘%(name)) pop.to_excel(u‘%s/Distance.xlsx‘%(name)) matrix = np.array(pop)*np.array(dist) total_commute = matrix.sum() commute = total_commute/np.array(pop).sum() print u‘%s 总通勤距离为 %s‘%(name,unicode(total_commute)) print u‘%s 通勤距离(ARC)为 %s‘%(name,unicode(commute)) print u‘%s 人口总数为 %s‘%(name,unicode(np.array(pop).sum())) for col in [u‘性别‘, u‘户籍‘, u‘职业‘, u‘收入‘]: for i in df.groupby(col).size().index: species = df[df[col]==i] pt = pd.pivot_table(data=species,values=col,rows=u‘工作地或学校地址‘, cols=u‘居住小区‘,aggfunc=np.size,fill_value=0) if sum(pt.shape) > 10: commuteCalcu(pt,distance,col+u‘中的‘+unicode(i)) else: print ‘%s_%s is too small‘%(col,i)
——————————————————————————————————————————————————
@Sugar_Lover
时间: 2024-10-10 04:46:28