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


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')

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM