Python Pandas – select_dtypes 與 dtypes

Loading

測試環境為 CentOS 8 (虛擬機)

可以透過 select_dtypes 與 dtypes 函數來指定只顯示那種資料格式的欄位, Pandas 所使用的資料格式如下.

參考資料 – https://note.nkmk.me/en/python-pandas-dtype-astype/

  • int8
    8-bit signed integer
  • int16
    16-bit signed integer
  • int32
    32-bit signed integer
  • int64
    64-bit signed integer
  • uint8
    8-bit unsigned integer
  • uint16
    16-bit unsigned integer
  • uint32
    32-bit unsigned integer
  • uint64
    64-bit unsigned integer
  • float16
    16-bit floating-point number
  • float32
    32-bit floating-point number
  • float64
    64-bit floating-point number
  • float128
    128-bit floating-point number
  • complex64
    64-bit complex floating-point number
  • complex128
    128-bit complex floating-point number
  • complex256
    256-bit complex floating-point number
  • bool
    Boolean (True or False)
  • unicode
    Unicode string
  • object
    Python objects

安裝所需模組

[root@localhost ~]# pip install pandas

匯入模組

[root@localhost ~]# python3
Python 3.6.8 (default, Sep 10 2021, 09:13:53)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd

資料範例.

df = pd.DataFrame({
    "Class":
    {
        "Ben": 'A1',
        "Alex": 'A1',
        "Jeff": 'B1',
        "Dexter": 'B1'
    },
    "Chinese":
    {
        "Ben": 68,
        "Alex": 86,
        "Jeff": 57,
        "Dexter": 95
    },
    "English":
    {
        "Ben": 63,
        "Alex": 92,
        "Jeff": 83,
        "Dexter": 89
    },
    "Math":
    {
        "Ben": 65,
        "Alex": 89,
        "Jeff": 77,
        "Dexter": 100
    }
})
>>> df
       Class  Chinese  English  Math
Ben       A1       68       63    65
Alex      A1       86       92    89
Jeff      B1       57       83    77
Dexter    B1       95       89   100

透過以下兩種方式.

select_dtypes

>>> df.select_dtypes(include=['int64','float64'])
        Chinese  English  Math
Ben          68       63    65
Alex         86       92    89
Jeff         57       83    77
Dexter       95       89   100
>>> list(df.select_dtypes(include=['int64','float64']))
['Chinese', 'English', 'Math']

dtypes

>>> df.dtypes
Class      object
Chinese     int64
English     int64
Math        int64
dtype: object
>>> df.columns
Index(['Class', 'Chinese', 'English', 'Math'], dtype='object')
>>> df.dtypes=='int64'
Class      False
Chinese     True
English     True
Math        True
dtype: bool
>>> df.columns[df.dtypes=='int64']
Index(['Chinese', 'English', 'Math'], dtype='object')
>>> list(df.columns[df.dtypes=='int64'])
['Chinese', 'English', 'Math']
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料