• 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	")
            #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')
    

      

  • 相关阅读:
    2019 Java 第四周总结
    2019第三周总结
    Java 第二周总结
    2019春第十二周作业
    Day3
    Day3
    Day3
    Day3
    Day2
    Day2
  • 原文地址:https://www.cnblogs.com/chen-blog/p/python_pandas.html
Copyright © 2020-2023  润新知