• 03.pandas数据DataFrame


    import pandas as pd
    #1.
    columns=["数学","英语","语文","理科综合","文科综合"]
    index=["top2","c9","985","211","1本","2本","3本","大专"]
    data={
        "数学":[145,140,135,130,125,120,115,100],
        "英语":[145,140,135,130,125,120,115,100],
        "语文":[145,140,135,130,125,120,115,100],
        "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
        # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    }
    frame=pd.DataFrame(data=data,columns=columns,index=index)#创建数据框架
    #空缺索引会使用默认数字索引
    
    #2.
    columns=["数学","英语","语文","理科综合","文科综合"]
    data={
        "数学":[145,140,135,130,125,120,115,100],
        "英语":[145,140,135,130,125,120,115,100],
        "语文":[145,140,135,130,125,120,115,100],
        "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
        # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    }
    frame=pd.DataFrame(data=data,columns=columns)#创建数据框架
    
    #一、取出列
    print(frame["数学"])
    print(frame.数学)#都是series类型
    print(frame[["数学","语文"]])#取出多列
    #print(frame["数学":"语文"])语文数学必须是数字
    
    #二、取出行 loc可以在[]内填写自定义索引;iloc不行,只能是数字索引
    print(frame.ix[0])#取出元素数字索引时取出第一行,其他索引需要把0换成其他
    print(frame.loc[0])#取出第一行元素
    print(frame.iloc[0])#取出第一行元素
    print(frame.ix[0:3])#取出多行
    print(frame.loc[["top2","c9"]])
    print(frame.loc[0:3])#取出多行,根据自定义索引
    print(frame.iloc[0:3])#取出多行,根据数据索引
    
    #三、取出多行多列
    print(frame[["数学","语文"]][0:3])#取出语文数学前三行
    print(frame[["数学","语文"]].loc[["top2","c9"]])
    print(frame[["数学","语文"]].iloc[0:2])
    
    #四、 取出元素
    print(frame["数学"][0])#取出元素
    print(frame.数学[0])#取出元素
    print(frame.iloc[0][0])#取出元素
    #一维数组是Serious 多行数组是DataFrame
    
    #3.
    columns=["数学","英语","语文","理科综合","文科综合"]
    index=["top2","c9","985","211","1本","2本","3本","大专"]
    data={
        "数学":[145,140,135,130,125,120,115,100],
        "英语":[145,140,135,130,125,120,115,100],
        "语文":[145,140,135,130,125,120,115,100],
        "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
        # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    }
    frame=pd.DataFrame(data=data,columns=columns,index=index)#创建数据框架
    

      

  • 相关阅读:
    socket的一个错误的解释SocketException以及其他几个常见异常
    lambda表达式和ef的语句转化
    多线程的异步委托初识
    跨线程访问控件的方法
    P1337 [JSOI2004]平衡点 / 吊打XXX
    P4099 [HEOI2013]SAO
    UVA10529 Dumb Bones
    P1291 [SHOI2002]百事世界杯之旅
    P2675 《瞿葩的数字游戏》T3-三角圣地
    P4363 [九省联考2018]一双木棋chess
  • 原文地址:https://www.cnblogs.com/wcyMiracle/p/12437030.html
Copyright © 2020-2023  润新知