1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Pandas中的幾個(gè)不常用的函數(shù)功能

        共 1185字,需瀏覽 3分鐘

         ·

        2022-01-02 10:29

        Pandas 是我們常用的一個(gè)Python數(shù)據(jù)分析庫(kù)。其中有不少操作是我們平時(shí)很少用到的。如下:

        數(shù)據(jù)源:

        df


        一、 獲取最大或最小值所對(duì)應(yīng)對(duì)象的名稱

        獲取某列的最小值:

        df["數(shù)量"].min()


        獲取最小值所對(duì)應(yīng)的汽車品牌:

        df[df["數(shù)量"] == df["數(shù)量"].min()]["品牌"].values[0]

        獲取最大值同理。

        ????????然而,我們可以使用.idxmax() 或 idxmin() 函數(shù)來(lái)更優(yōu)雅得實(shí)現(xiàn)以上功能:

        df.loc[df["數(shù)量"].idxmin()]["品牌"]

        可見(jiàn),所得結(jié)果一致,并且語(yǔ)句顯得更加簡(jiǎn)短優(yōu)雅。

        二、 數(shù)據(jù)切片

        用 .cut() 函數(shù)將數(shù)據(jù)區(qū)間 [10,80] 均等地分成10份,等差為 (80 - 10) / 10 = 7.

        pd.cut(df["數(shù)量"],10)


        查看當(dāng)前數(shù)據(jù)所屬的劃分區(qū)間:

        pd.cut(df["數(shù)量"],10,labels = False)

        三、 用 .nsmallest() 或 .nlargest() 獲取前幾的最小或最大值。

        比如獲取數(shù)量最小的前3條記錄:

        df[["國(guó)家", "品牌", "數(shù)量"]].nsmallest(3, "數(shù)量")


        再跟常用的方法對(duì)比一下,如下:

        df.sort_values(by = "數(shù)量",ascending = True).head(3)


        注意事項(xiàng):.nsmallest() 或 .nlargest() 參數(shù)里的篩選條件一定要包好在所抽取出來(lái)的DataFrame里面。

        四、 數(shù)據(jù)透視表

        4.1 普通的篩選求和:

        df[(df["國(guó)家"] == "中國(guó)") & (df["品牌"] == "五菱")]["數(shù)量"].sum()


        4.2 pivot_table

        pd.pivot_table(df, values = "數(shù)量", index = "品牌", columns = "國(guó)家", aggfunc = "sum").fillna(0)



        4.2 pivot_table + unstack

        pd.pivot_table(df, values = "數(shù)量", index = "品牌", columns = "國(guó)家", aggfunc = "sum").fillna(0).unstack()




        相關(guān)閱讀:


        瀏覽 55
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            日韩在线三级片 | 欧美天堂影院 | 中国操比 | 99干逼 | 逼逼爱 | 中文字幕在线观看网站 | 爱搞搞搞 | 国产美女永久无遮挡 | 欧美日性爱 | 靠比视频网站 |