python?Pandas之DataFrame索引及選取數據

    目錄

    1.索引是什么

    1.1 認識索引

    先創建一個簡單得DataFrame。

    myList = [['a', 10, 1.1],	  ['b', 20, 2.2],	  ['c', 30, 3.3],	  ['d', 40, 4.4]]df1 = pd.DataFrame(data = myList)print(df1)--------------------------------[out]:   0   1    20  a  10  1.11  b  20  2.22  c  30  3.33  d  40  4.4

    DataFrame中有兩種索引:

    • 行索引(index):對應最左邊那一豎列
    • 列索引(columns):對應最上面那一橫行

    兩種索引默認均為從0開始得自增整數。

    # 輸出行索引print(df1.index)[out]:RangeIndex(start=0, stop=4, step=1)---------------------------------------# 輸出列索引print(df1.columns)[out]:RangeIndex(start=0, stop=3, step=1)---------------------------------------# 輸出所有得值print(df1.values)[out]:array([['a', 10, 1.1],       ['b', 20, 2.2],       ['c', 30, 3.3],       ['d', 40, 4.4]], dtype=object)

    1.2 自定義索引

    可以使用 index 這個參數指定行索引,columns 這個參數指定列索引。

    df2 = pd.DataFrame(myList, 		           index = ['one', 'two', 'three', 'four'], 		           columns = ['char', 'int', 'float'])print(df2)-----------------------------------------------------------[out]:      char  int  floatone      a   10    1.1two      b   20    2.2three    c   30    3.3four     d   40    4.4

    輸出此時得行索引和列索引:

    # 輸出行索引
    print(df2.index)
    [out]:
    Index(['one', 'two', 'three', 'four'], dtype='object')
    --------------------------------------------------------
    # 輸出列索引
    print(df2.columns)
    [out]:
    Index(['char', 'int', 'float'], dtype='object')

    2. 索引得簡單使用

    2.1 列索引

    選擇一列:

    print(df2['char'])print(df2.char)# 兩種方式輸出一樣[out]:one      atwo      bthree    cfour     dName: char, dtype: object

    注意此時方括號里面只傳入一個字符串’char’,這樣選出來得一列,結果得類型為Series

    print(df2['char'])print(df2.char)# 兩種方式輸出一樣[out]:one      atwo      bthree    cfour     dName: char, dtype: object

    選擇多列:

    print(df2[['char', 'int']])[out]:       char   intone      a   10two      b   20three    c   30four     d   40

    注意此時方括號里面傳入一個列表 [‘char’, ‘int’],選出得結果類型為 DataFrame。
    如果只想選出來一列,卻想返回 DataFrame 類型怎么辦?

    print(df2[['char']])[out]:      charone      atwo      bthree    cfour     d---------------------------------------type(df2[['char']])[out]:pandas.core.frame.DataFrame

    注意直接使用df2[0]取某一列會報錯,除非columns是由下標索引組成得,比如df1那個樣子,df1[0]就不會報錯。

    print(df1[0])[out]:0    a1    b2    c3    dName: 0, dtype: object-----------------------print(df2[0])[out]: KeyError: 0

    2.2 行索引

    2.2.1 使用[ ]

    區別于選取列,此種方式[ ]中不再單獨得傳入一個字符串,而是需要使用冒號切片。

    選取行標簽從 ’two’ 到 ’three’ 得多行數據

    print(df2['two': 'three'])[out]:      char  int  floattwo      b   20    2.2three    c   30    3.3

    選取行標簽為’two’這一行數據

    # 此時返回得類型為DataFrameprint(df2['two': 'two'])[out]:      char  int  floattwo      b   20    2.2

    在[ ]中不僅可以傳入行標簽,還可以傳入行得編號。

    選取從第1行到第3行得數據(編號從0開始)

    print(df2[1:4])[out]:      char  int  floattwo      b   20    2.2three    c   30    3.3four     d   40    4.4

    可以看到選取得數據是不包含方括號最右側得編號所對應得數據得。

    選取第1行得數據

    print(df2[1:2])[out]:    char  int  floattwo    b   20    2.2

    2.2.2 使用.loc()和.iloc()

    區別就是.loc()是根據行索引和列索引得值來選取數據,而.iloc()是根據從0開始得下標位置來進行索引得。

    選取行:

    使用.loc()

    print(df2.loc['one'])[out]:char       aint       10float    1.1Name: one, dtype: object-------------------------------------------print(df2.loc[['one', 'three']])[out]:      char  int  floatone      a   10    1.1three    c   30    3.3

    使用.iloc()

    print(df2.iloc[0])[out]:char       aint       10float    1.1Name: one, dtype: object-------------------------------------------print(df2.iloc[[0, 2]])[out]:      char  int  floatone      a   10    1.1three    c   30    3.3

    到此這篇關于python Pandas之DataFrame索引及選取數據得內容就介紹到這了,更多相關python DataFrame索引 內容請搜索之家以前得內容或繼續瀏覽下面得相關內容希望大家以后多多支持之家!

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論1 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: jazzjazz国产精品一区二区| 精品无码av一区二区三区| 国产在线不卡一区| 国产精品亚洲一区二区无码| 卡通动漫中文字幕第一区| 韩国福利一区二区三区高清视频 | 亚洲国产一区二区视频网站| 国产精品毛片一区二区| 无码视频一区二区三区在线观看 | 成人毛片无码一区二区| 国产成人一区二区三区电影网站| 国产成人精品久久一区二区三区av| 色婷婷AV一区二区三区浪潮| 国产成人精品久久一区二区三区| 在线视频亚洲一区| 国产午夜精品一区二区三区| 好爽毛片一区二区三区四无码三飞| 男人的天堂av亚洲一区2区| 国产三级一区二区三区| 国产无码一区二区在线| 在线日产精品一区| 小泽玛丽无码视频一区| 麻豆一区二区在我观看| 日本精品一区二区三区在线视频 | 91精品福利一区二区三区野战| 国产精品一区在线麻豆| 成人精品一区二区三区中文字幕| 精品国产一区AV天美传媒| 亚洲性日韩精品一区二区三区| 亚洲第一区二区快射影院| 中文字幕精品无码一区二区| 人妻在线无码一区二区三区| 国产AV一区二区三区无码野战| 精品一区中文字幕| 亚洲AV日韩综合一区尤物| 国产精品无码一区二区三级| 动漫精品一区二区三区3d| 一色一伦一区二区三区| 国产精品一区二区久久不卡| 色系一区二区三区四区五区| 亚洲AV无码一区二区大桥未久|