pandas.DataFrame中提取特定類型dtype的列
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ù))。 (標準位數(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
當然,當最多包括位數(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標準內(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的元素,因此不必擔心太多。
指定要排除的類型:參數(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'>})
到此這篇關于pandas.DataFrame中提取特定類型dtype的列的文章就介紹到這了,更多相關pandas DataFrame提取特定類型列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Pandas.DataFrame時間序列數(shù)據(jù)處理的實現(xiàn)
- Pandas通過index選擇并獲取行和列
- Pandas中MultiIndex選擇并提取任何行和列
- pandas中按行或列的值對數(shù)據(jù)排序的實現(xiàn)
- Pandas數(shù)據(jù)查詢的集中實現(xiàn)方法
- pandas讀取Excel批量轉(zhuǎn)換時間戳的實踐
- Python?中?Pandas?文件操作和讀取?CSV?參數(shù)詳解
- Pandas merge合并兩個DataFram的實現(xiàn)
- 針對Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
相關文章
深入解析Python中的__builtins__內(nèi)建對象
__builtins__ 是內(nèi)建模塊__builtin__中的對象,使用Python中的內(nèi)建函數(shù)時會通過__builtins__引導,這里我們就來深入解析Python中的__builtins__內(nèi)建對象,需要的朋友可以參考下2016-06-06pygame實現(xiàn)鍵盤的連續(xù)監(jiān)控
這篇文章主要為大家詳細介紹了pygame實現(xiàn)鍵盤的連續(xù)監(jiān)控,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04