zoukankan      html  css  js  c++  java
  • Pandas入门之九:数据选择

    已信任
    Jupyter 服务器: 本地
    Python 3: Not Started
    [36]
    
    
    
    import pandas as pd
    import numpy as np
    [37]
    
    
    
    # .loc基于标签
    # .iloc基于索引
    
    
    
    [39]
    
    
    
    
    df = pd.DataFrame(np.random.randn(8,4),index=['a','b','c','d','e','f','g','h'],columns=['A','B','C','D'])
    df
    A    B    C    D
    a    -0.629391    -2.199736    0.547864    0.137235
    b    0.346160    0.648808    0.290378    -0.598081
    c    1.658532    1.114108    -1.595136    0.196397
    d    -0.760715    -0.186863    1.043977    0.287979
    e    0.220411    -1.352767    0.924732    -0.456502
    f    -1.952481    1.228079    0.900851    0.597607
    g    -1.683863    1.276186    -0.551237    -0.278381
    h    0.083268    1.494524    0.162098    0.414894
    [40]
    
    
    
    # 选择A和B列所有内容
    df.loc[:,['A','B']]
    A    B
    a    -0.629391    -2.199736
    b    0.346160    0.648808
    c    1.658532    1.114108
    d    -0.760715    -0.186863
    e    0.220411    -1.352767
    f    -1.952481    1.228079
    g    -1.683863    1.276186
    h    0.083268    1.494524
    [41]
    
    
    
    # 选择标签选择a-h行
    df.loc['a':'e']
    A    B    C    D
    a    -0.629391    -2.199736    0.547864    0.137235
    b    0.346160    0.648808    0.290378    -0.598081
    c    1.658532    1.114108    -1.595136    0.196397
    d    -0.760715    -0.186863    1.043977    0.287979
    e    0.220411    -1.352767    0.924732    -0.456502
    [42]
    
    
    
    # 选择任意行,任意列
    df.loc['a':'e','B':]
    B    C    D
    a    -2.199736    0.547864    0.137235
    b    0.648808    0.290378    -0.598081
    c    1.114108    -1.595136    0.196397
    d    -0.186863    1.043977    0.287979
    e    -1.352767    0.924732    -0.456502
    [43]
    
    
    
    # 取出标签a中大于1的数据
    df.loc['a']>1
    A    False
    B    False
    C    False
    D    False
    Name: a, dtype: bool
    [44]
    
    
    
    df.loc[:,'A']>1
    a    False
    b    False
    c     True
    d    False
    e    False
    f    False
    g    False
    h    False
    Name: A, dtype: bool
    [49]
    
    
    
    df.loc[:,df.loc['d']>1]
            C
    a    0.547864
    b    0.290378
    c    -1.595136
    d    1.043977
    e    0.924732
    f    0.900851
    g    -0.551237
    h    0.162098
    [50]
    
    
    
    # iloc基于位置的索引,第1行数据
    df.iloc[0]
    A   -0.629391
    B   -2.199736
    C    0.547864
    D    0.137235
    Name: a, dtype: float64
    [51]
    
    
    
    df.iloc[1]
    A    0.346160
    B    0.648808
    C    0.290378
    D   -0.598081
    Name: b, dtype: float64
    [55]
    
    
    
    df.iloc[0:3,1:3]
    B    C
    a    -2.199736    0.547864
    b    0.648808    0.290378
    c    1.114108    -1.595136
    [56]
    
    
    
    # 取任意行和任意列
    df.iloc[0:3,[1,3]]
              B           D
    a    -2.199736    0.137235
    b    0.648808    -0.598081
    c    1.114108    0.196397
    [58]
    
    
    
    # 快速获取数据
    df.A
    a   -0.629391
    b    0.346160
    c    1.658532
    d   -0.760715
    e    0.220411
    f   -1.952481
    g   -1.683863
    h    0.083268
    Name: A, dtype: float64
    [-]
  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/vvzhang/p/15012998.html
Copyright © 2011-2022 走看看