網(wǎng)站首頁 編程語言 正文
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
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-05-15 require和import的區(qū)別詳解
- 2023-01-17 關(guān)于最大池化層和平均池化層圖解_python
- 2022-03-24 .Net?Core服務(wù)治理Consul自動擴展和服務(wù)調(diào)用_自學(xué)過程
- 2023-12-12 TCP通信的實現(xiàn)-優(yōu)化點對點聊天
- 2023-07-22 SpringBoot中@Cacheable如何使用
- 2024-03-05 git的使用
- 2022-08-19 Python運行時修改業(yè)務(wù)SQL代碼_python
- 2022-12-24 Docker網(wǎng)絡(luò)模型以及容器通信詳解續(xù)篇_docker
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支