網站首頁 編程語言 正文
pandas.DataFrame為每一列保存一個數據類型dtype。
要僅提取(選擇)特定數據類型為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'>})
原文鏈接:https://blog.csdn.net/qq_18351157/article/details/109745683
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-27 Docker使用Calico網絡模式配置及問題處理方法_docker
- 2023-01-02 C++?命名空間?using聲明使用示例詳解_C 語言
- 2022-01-28 mybatis事務DefaultSqlSession-策略模式
- 2022-08-30 詳解Python單元測試的兩種寫法_python
- 2022-07-21 SQL查詢出的兩列合并成一列顯示
- 2023-11-13 【云原生】docker設置非root用戶使用權限的方法
- 2022-06-20 一文搞懂Python的hasattr()、getattr()、setattr()?函數用法_pyth
- 2022-09-03 golang架構設計開閉原則手寫實現_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支