当前位置:   article > 正文

pandas处理数据例子_kaggle test 图像数据解析pandas python 读取csv 某行某列

kaggle test 图像数据解析pandas python 读取csv 某行某列

API : pandas api

import  pandas as pd
import os
import numpy as np
# apply适用的函数,处理每个group
def add_prop(group):
    births = group.births.astype(float)
    group['prop'] = births / births.sum()
    return group
# top1000
def top1000(group):
    return group.sort_values(by="births",ascending=False)[:1000]
if __name__ == "__main__":
    years = range(1880,2011)
    names = ["name","sex","births"]
    pieces = []
    # read sequence file
    for year in years:
        path = "names/yob{year}.txt".format(year=year)
        if os.path.exists(path):
            print "begin to read {path}".format(path=path)
        else:
            print "{path} does not exists".format(path=path)
            continue
        # 读取数据
        frame = pd.read_table(path,names=names,sep=',')
        frame['years'] = year
        pieces.append(frame)

    names = pd.concat(pieces,ignore_index=True)
    #print names[:10]
    #数据归总=========================
    total_births = names.pivot_table("births",index="years",columns="sex",aggfunc=sum)
    total_births.plot(title="births")
    #print total_births

    grouped = names.groupby(['years','sex']).size().unstack()
    #print grouped

    #apply
    # group函数以及apply配合使用
    names = grouped = names.groupby(['years','sex']).apply(add_prop)
    print names[:10]
    ## verify
    print np.allclose(names.groupby(['years','sex']).prop.sum(),1)
    ## every year top 1000
    grouped = names.groupby(['years','sex'])
    top1000 = grouped.apply(top1000)
    #print top1000.ix[top1000['years']==2000]
    print top1000.ix[top1000['years']==2000]
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/792049
推荐阅读
相关标签
  

闽ICP备14008679号