zoukankan      html  css  js  c++  java
  • DataFrame查找

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

    loc (根据索引名称取数据 , 适合多列)

    iloc (根据索引序号取数据,   适合多列)

    at  (和loc类似,只用于取单列, 性能更好)

    iat (和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']]
    # 取多列
    print df.loc['A', ['b', 'c']]
    #----------------------------------------------------------------------------

    #--------------------iLoc的用法-----------------------------------------------
    # 取第1行
    print df.iloc[0]
    # 取第1行列名 'b'
    print df.iloc[0, [1]]
    # 取多列
    print df.iloc[0, [1, 2]]
    #----------------------------------------------------------------------------

    #--------------------at的用法-----------------------------------------------
    print df.at["A", 'a']
    #---------------------------------------------------------------------------

    #--------------------iat的用法-----------------------------------------------
    print df.iat[0, 0]
    #----------------------------------------------------------------------------

    #--------------------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, 使用名称查找

    二  获取索引和字段名

    #--------------------获取索引-----------------------------------------------
    print df.index[0]
    #--------------------------------------------------------------------------
    
    #--------------------获取列名-----------------------------------------------
    print df.columns[0]
    #--------------------------------------------------------------------------
  • 相关阅读:
    golang中的值传递和引用传递
    链表

    hashtable(哈希表)
    TAO: Facebook’s Distributed Data Store for the Social Graph. 论文阅读笔记(上)
    Skip Lists: A Probabilistic Alternative to Balanced Trees 跳表论文阅读笔记
    【译】如何实现一个现代化电子商城搜索?(一)
    Elasticsearch搜索资料汇总
    Python 按比例获取样本数据或执行任务
    Python 按分类样本数占比生成并随机获取样本数据
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/7670122.html
Copyright © 2011-2022 走看看