<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          3 個(gè)不常見但非常實(shí)用的Pandas 使用技巧

          共 2457字,需瀏覽 5分鐘

           ·

          2022-05-26 21:05


          來源:DeepHub IMBA
          本文共1000字,建議閱讀5分鐘
          本文為你演示一些不常見,但是卻非常有用的 Pandas 函數(shù)。


          創(chuàng)建一個(gè)示例 DataFrame 。

          import numpy as npimport pandas as pddf = pd.DataFrame({   "date": pd.date_range(start="2021-11-20", periods=100, freq="D"),  "class": ["A","B","C","D"] * 25,  "amount": np.random.randint(10, 100, size=100)})
          df.head()

          我們創(chuàng)建有一個(gè) 3 列 100 行的 DataFrame。date 列包含 100 個(gè)連續(xù)日期,class 列包含 4 個(gè)以對(duì)象數(shù)據(jù)類型存儲(chǔ)的不同值,amount 列包含 10 到 100 之間的隨機(jī)整數(shù)。

          1. To_period


          在 Pandas 中,操作 to_period 函數(shù)允許將日期轉(zhuǎn)換為特定的時(shí)間間隔。使用該方法可以獲取具有許多不同間隔或周期的日期,例如日、周、月、季度等。

          比如針對(duì)于時(shí)間類型的列,month 方法只返回在許多情況下沒有用處的月份的數(shù)值,我們無法區(qū)分 2020 年 12 月和 2021 年 12 月。但是我們通過使用to_period 函數(shù)的參數(shù)”M“實(shí)現(xiàn)時(shí)間序列。

          讓我們?yōu)槟暝潞图径葎?chuàng)建新列。

          df["month"] = df["date"].dt.to_period("M")df["quarter"] = df["date"].dt.to_period("Q")
          df.head()

          還可以查看 DataFrame 中不同的年月和季度值。

          df["month"].value_counts()# output2021-12   312022-01   312022-02   272021-11   11Freq: M, Name: month, dtype: int64
          --------------------------df["quarter"].value_counts()# output2022Q1 582021Q4 42Freq: Q-DEC, Name: quarter, dtype: int64

          2. Cumsum 和 groupby


          cumsum 是一個(gè)非常有用的 Pandas 函數(shù)。它計(jì)算列中值的累積和。以下是我們通常的使用方式:

          df["cumulative_sum"] = df["amount"].cumsum()df.head()

          這樣就獲得了金額列的列值累積總和。但是它只是全部的總和沒有考慮分類。在某些情況下,我們可能需要分別計(jì)算不同類別的累積和。

          Pandas中我們只需要按類列對(duì)行進(jìn)行分組,然后應(yīng)用 cumsum 函數(shù)。

          df["class_cum_sum"] = df.groupby("class")["amount"].cumsum()


          讓我們查看 A 類的結(jié)果。


          df[df["class"]=="A"].head()


          類的累積總和列包含為每個(gè)類單獨(dú)計(jì)算的累積值總和。

          3. Category數(shù)據(jù)類型


          我們經(jīng)常需要處理具有有限且固定數(shù)量的值的分類數(shù)據(jù)。例如在我們的 DataFrame 中,”分類“列具有 4 個(gè)不同值的分類變量:A、B、C、D。

          默認(rèn)情況下,該列的數(shù)據(jù)類型為object。

          df.dtypes
          # outputdate datetime64[ns]class objectamount int64month period[M]quarter period[Q-DEC]cumulative_sum int64class_cum_sum int64

          Pandas 還有一個(gè)“Category”數(shù)據(jù)類型,它比object數(shù)據(jù)類型消耗更少的內(nèi)存。因此最好盡可能使用category數(shù)據(jù)類型。

          df["class_category"] = df["class"].astype("category")df.dtypes
          # outputdate datetime64[ns]class objectamount int64month period[M]quarter period[Q-DEC]cumulative_sum int64class_cum_sum int64class_category categorydtype: object

          現(xiàn)在可以比較 class 和 class_category 列的內(nèi)存消耗。

          df.memory_usage()
          # outputIndex 128date 800class 800amount 800month 800quarter 800cumulative_sum 800class_cum_sum 800class_category 304dtype: int64

          class_category 列消耗的內(nèi)存不到 class 列的一半。差異是 496 字節(jié),雖然并不多。但是當(dāng)我們使用大型數(shù)據(jù)集時(shí),這樣差異就會(huì)被放大,這樣就變成了節(jié)省大量的空間。

          作者:Soner Y?ld?r?m
          編輯:黃繼彥



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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  可以看的免费黄色片 | 欧美成人视频 | 欧美国产成人精品一区二区三区 | 91麻豆精品一区二区三区 | 日韩三级片在线播放 |