1. <em id="yud1w"><acronym id="yud1w"><u id="yud1w"></u></acronym></em>
      
      
      <button id="yud1w"></button>

      python教程

      當前位置:首頁?>?Pandas教程?>?當前文章

      Pandas教程

      DataFrame的iloc位置索引,切片,布爾索引篩選單多行多列

      2021-10-25 159贊 老董筆記
      每篇文章努力于解決一個問題!更多精品可移步文章底部。

        pandas中DataFrame選擇數據可以用iloc選擇器,注意iloc不是函數。iloc用于位置索引篩選,也可以用于布爾值篩選。

        官方介紹如下:

        Purely integer-location based indexing for selection by position。.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

        我們按照這個df為例來進行學習。

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df)
      
        pingtai   pv   uv
      a   baidu  800  200
      b   sogou  500  100
      c  google  100   60
      

        1、iloc位置索引選擇單行,返回Series或者DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[0])
      print('===')
      print(df.iloc[[0]])
      
      pingtai    baidu
      pv           800
      uv           200
      Name: a, dtype: object
      ===
        pingtai   pv   uv
      a   baidu  800  200
      

        2、iloc位置索引選擇多行及所有列,返回DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[[0,1]])
      
        pingtai   pv   uv
      a   baidu  800  200
      b   sogou  500  100
      

        3、iloc位置索引選擇多行多列,返回DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[[0,1],[1,2]])
      
          pv   uv
      a  800  200
      b  500  100
      

        4、iloc位置切片索引(末端不包含)選擇多行及所有列,返回DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[[0,1],:])
      
        pingtai   pv   uv
      a   baidu  800  200
      b   sogou  500  100
      

        5、iloc位置切片索引(末端不包含)選擇多行多列,返回DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[0:2,0:2])
        pingtai   pv
      a   baidu  800
      b   sogou  500
      

        6、iloc布爾索引(末端不包含)選擇多行多列,返回DataFrame

      
      df = pd.DataFrame([['baidu','800','200'],['sogou','500','100'],['google','100','60']],columns=['pingtai','pv','uv'],index=['a','b','c'])
      print(df.iloc[[True,True,True],[False,True,True]])
          pv   uv
      a  800  200
      b  500  100
      c  100   60
      

      文章評論

      DataFrame的iloc位置索引,切片,布爾索引篩選單多行多列文章寫得不錯,值得贊賞
      国产99视频精品免视看6