zoukankan      html  css  js  c++  java
  • DataFrame操作

    一 通过索引取数据 (ix/loc/iloc)

    loc (根据索引名称取数据)

    iloc (根据索引序号取数据)

    ix  (综合两者)

    data = [[1,2,3],[4,5,6]]
    index = ['A','B']
    columns=['a','b','c']
    df = pd.DataFrame(data=data, index=index, columns=columns)
    
    #--------------------Loc的用法-----------------------------------------------
    # 取第1行
    print df.loc['A']
    # 取第1行列名 'b'
    print df.loc['A']['b']
    #----------------------------------------------------------------------------
    
    #--------------------iLoc的用法-----------------------------------------------
    # 取第1行
    print df.iloc[0]
    # 取第1行列名 'b'
    print df.iloc[0][1]
    #----------------------------------------------------------------------------
    
    #--------------------ix的用法-----------------------------------------------
    # 取第1行
    print df.ix[0]
    # 取第1行列名 'b'
    print df.ix[0][1]
    
    # 取第1行
    print df.ix['A']
    # 取第1行列名 'b'
    print df.ix['A']['b']
    #----------------------------------------------------------------------------
    

      需要注意的地方,1 该类用法必须先通过索引,取到行(series)再取列数据, 直接取列数据会报错  2 通过ix获取数据时,如果索引为int, 则识别为loc, 使用名称查找

  • 相关阅读:
    项目流程
    Html5 经验
    knockoutjs 经验总结
    redmine处理规范
    用fiddler监控移动端的通讯
    git
    es6 中的 Promise
    html5游戏的横屏问题
    jQuery 学习笔记
    jQuery 里的 Promise
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/7670111.html
Copyright © 2011-2022 走看看