溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

pandas.DataFrame中如何提取特定類型dtype的列

發布時間:2023-02-23 16:15:53 來源:億速云 閱讀:124 作者:iii 欄目:開發技術

本篇內容介紹了“pandas.DataFrame中如何提取特定類型dtype的列”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

pandas.DataFrame為每一列保存一個數據類型dtype。

要僅提?。ㄟx擇)特定數據類型為dtype的列,請使用pandas.DataFrame的select_dtypes()方法。

以帶有各種數據類型的列的pandas.DataFrame為例。

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 1, 3],
                   'b': [0.4, 1.1, 0.1, 0.8],
                   'c': ['X', 'Y', 'X', 'Z'],
                   'd': [[0, 0], [0, 1], [1, 0], [1, 1]],
                   'e': [True, True, False, True]})

df['f'] = pd.to_datetime(['2018-01-01', '2018-03-15', '2018-02-20', '2018-03-15'])

print(df)
#    a    b  c       d      e          f
# 0  1  0.4  X  [0, 0]   True 2018-01-01
# 1  2  1.1  Y  [0, 1]   True 2018-03-15
# 2  1  0.1  X  [1, 0]  False 2018-02-20
# 3  3  0.8  Z  [1, 1]   True 2018-03-15

print(df.dtypes)
# a             int64
# b           float64
# c            object
# d            object
# e              bool
# f    datetime64[ns]
# dtype: object

將描述以下內容。

select_dtypes()的基本用法

  • 指定要提取的類型:參數include

  • 指定要排除的類型:參數exclude

select_dtypes()的基本用法

指定要提取的類型:參數include

在參數include中指定要提取的數據類型dtype。

print(df.select_dtypes(include=int))
#    a
# 0  1
# 1  2
# 2  1
# 3  3

可以按原樣指定作為Python的內置類型提供的那些變量,例如int和float。您可以將“ int”指定為字符串,也可以指定“ int64”(包括確切位數)。 (標準位數取決于環境)

print(df.select_dtypes(include='int'))
#    a
# 0  1
# 1  2
# 2  1
# 3  3

print(df.select_dtypes(include='int64'))
#    a
# 0  1
# 1  2
# 2  1
# 3  3

當然,當最多包括位數時,除非位數匹配,否則不會選擇它。

print(df.select_dtypes(include='int32'))
# Empty DataFrame
# Columns: []
# Index: [0, 1, 2, 3]

列表中可以指定多種數據類型dtype。日期和時間datetime64 [ns]可以由’datetime’指定。

print(df.select_dtypes(include=[int, float, 'datetime']))
#    a    b          f
# 0  1  0.4 2018-01-01
# 1  2  1.1 2018-03-15
# 2  1  0.1 2018-02-20
# 3  3  0.8 2018-03-15

可以將數字類型(例如int和float)與特殊值“ number”一起指定。

print(df.select_dtypes(include='number'))
#    a    b
# 0  1  0.4
# 1  2  1.1
# 2  1  0.1
# 3  3  0.8

元素為字符串str類型的列的數據類型dtype是object,但是object列還包含除str外的Python標準內置類型。實際上,數量并不多,但是,如示例中所示,如果有一列的元素為列表類型,請注意,該列也是由include = object提取的。

print(df.select_dtypes(include=object))
#    c       d
# 0  X  [0, 0]
# 1  Y  [0, 1]
# 2  X  [1, 0]
# 3  Z  [1, 1]

print(type(df.at[0, 'c']))
# <class 'str'>

print(type(df.at[0, 'd']))
# <class 'list'>

但是,除非對其進行有意處理,否則字符串str類型以外的對象都不會(可能)成為pandas.DataFrame的元素,因此不必擔心太多。

指定要排除的類型:參數exclude

在參數exclude中指定要排除的數據類型dtype。您還可以在列表中指定多個數據類型dtype。

print(df.select_dtypes(exclude='number'))
#    c       d      e          f
# 0  X  [0, 0]   True 2018-01-01
# 1  Y  [0, 1]   True 2018-03-15
# 2  X  [1, 0]  False 2018-02-20
# 3  Z  [1, 1]   True 2018-03-15

print(df.select_dtypes(exclude=[bool, 'datetime']))
#    a    b  c       d
# 0  1  0.4  X  [0, 0]
# 1  2  1.1  Y  [0, 1]
# 2  1  0.1  X  [1, 0]
# 3  3  0.8  Z  [1, 1]

可以同時指定包含和排除,但是如果指定相同的類型,則會發生錯誤。

print(df.select_dtypes(include='number', exclude=int))
#      b
# 0  0.4
# 1  1.1
# 2  0.1
# 3  0.8

# print(df.select_dtypes(include=[int, bool], exclude=int))
# ValueError: include and exclude overlap on frozenset({<class 'numpy.int64'>})

“pandas.DataFrame中如何提取特定類型dtype的列”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女