您的位置:首页 > 编程语言 > Python开发

python 使用pandas,完成对excel的操作: 遍历,求偏度(skew)的小程序

2015-03-03 14:12 821 查看
excel有针对偏度的计算函数 skew(), 但是不清楚怎么使用excel进行遍历, 数据量很大。

尝试使用python进行解决。

第一次学习python,没想到了在克服安装各种包的难过之后,居然成功实现了。

python3.3:

#this is a test case

# -*- coding: gbk -*-
print("hello python!中文")

#env config
import xlrd
import os
import xlwt3
import numpy

import pandas as pd
#from pandas import Series,DataFrame
#import pandas

data = xlrd.open_workbook("E:\\data.xlsx")
table = data.sheets()[0]   #this need to be verify more

#print ("check")
print (table.nrows)
print (table.name)
print ("############################")

#total line num
line_num=table.nrows

cell_sectionA=table.cell(1,0).value
cell_sectionB=table.cell(1,1).value
#print (cell_sectionA)
#print (cell_sectionB)

start_value=cell_sectionA

#we need to recode the start value ,but not the end.
sectionB_each_time_start=0
#sectionB_each_time_end=i is ok.

for i in range(1,line_num):
if start_value != table.cell(i,0).value:
cacu_num=i-sectionB_each_time_start;
#print (cacu_num)
#print ("********************************")
data={}
for j in range(0,(cacu_num-1)):
data[j]= table.cell((sectionB_each_time_start+j+1),1).value
#print (data[j])

df = pd.Series(data)
#print("skew\t")
#print("skew: %d  %f" %(table.cell(sectionB_each_time_start+1,0).value,df.skew()))
print("%d"%table.cell(sectionB_each_time_start+1,0).value)
#print("%f" %df.skew())

#after caculate ,update the variable.
sectionB_each_time_start=i-1
start_value=table.cell(i,0).value

#file=xlwt3.Workbook()
#table_for_wt=file.add_sheet("test1");

#table_for_wt.write(0,0,cell_b)
#table_for_wt.write(1,1,cell_b)
#file.save('E:\\wtest.xls')


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: