日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

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

作者:餃子大人 ? 更新時間: 2023-06-04 編程語言

pandas.DataFrame為每一列保存一個數(shù)據(jù)類型dtype。

要僅提取(選擇)特定數(shù)據(jù)類型為dtype的列,請使用pandas.DataFrame的select_dtypes()方法。

以帶有各種數(shù)據(jù)類型的列的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

將描述以下內(nèi)容。

select_dtypes()的基本用法

  • 指定要提取的類型:參數(shù)include
  • 指定要排除的類型:參數(shù)exclude

select_dtypes()的基本用法

指定要提取的類型:參數(shù)include

在參數(shù)include中指定要提取的數(shù)據(jù)類型dtype。

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

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

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

當(dāng)然,當(dāng)最多包括位數(shù)時,除非位數(shù)匹配,否則不會選擇它。

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

列表中可以指定多種數(shù)據(jù)類型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

可以將數(shù)字類型(例如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類型的列的數(shù)據(jù)類型dtype是object,但是object列還包含除str外的Python標(biāo)準(zhǔn)內(nèi)置類型。實際上,數(shù)量并不多,但是,如示例中所示,如果有一列的元素為列表類型,請注意,該列也是由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的元素,因此不必?fù)?dān)心太多。

指定要排除的類型:參數(shù)exclude

在參數(shù)exclude中指定要排除的數(shù)據(jù)類型dtype。您還可以在列表中指定多個數(shù)據(jù)類型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]

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

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'>})

原文鏈接:https://blog.csdn.net/qq_18351157/article/details/109745683

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新