Pandas DataFrame.filter(~) 方法返回標簽與指定模式匹配的行或列。
警告
該方法根據列/行的標簽而不是實際數據應用過濾。
參數
1.items | list-like | optional
提取 items 中包含標簽的行或列。
2. like | string | optional
提取標簽包含 like 的行或列。
3. regex | string(正則表達式)| optional
提取標簽與 regex 匹配的行或列。
4. axis | int 或 string | optional
是否提取行或列:
| 軸 | 說明 | 
|---|---|
| 
 | 提取行。 | 
| 
 | 提取列。 | 
默認情況下,axis=1 。
警告
您隻能指定 items 、 like 和 regex 之一。
返回值
一個新的DataFrame,其中包含標簽與指定模式匹配的行或列。
例子
考慮以下 DataFrame :
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6],"C":[7,8,9]}, index=["a","b","c"])
df
   A  B  C
a  1  4  7
b  2  5  8
c  3  6  9指定項目
要獲取列 A 和 C :
df.filter(items=["A","C"])
   A  C
a  1  7
b  2  8
c  3  9獲取行
要獲取行 a 和 c :
df.filter(items=["a","c"], axis=0)
   A  B  C
a  1  4  7
c  3  6  9指定 like 和 regex
考慮以下 DataFrame :
df = pd.DataFrame({"ABC":[1,2,3],"BCD":[4,5,6],"E":[7,8,9]}, index=["a","b","c"])
df
   ABC  BCD  E
a   1    4   7
b   2    5   8
c   3    6   9要獲取標簽包含子字符串 "BC" 的列:
df.filter(like="BC")
   ABC  BCD
a   1    4
b   2    5
c   3    6要獲取標簽包含字符 "B" 的列:
df.filter(regex=".*B.*")
   ABC  BCD
a   1    4
b   2    5
c   3    6相關用法
- Python PySpark DataFrame filter方法用法及代碼示例
- Python Pandas DataFrame fillna方法用法及代碼示例
- Python PySpark DataFrame fillna方法用法及代碼示例
- Python Pandas DataFrame first_valid_index方法用法及代碼示例
- Python Pandas DataFrame first方法用法及代碼示例
- Python Pandas DataFrame floordiv方法用法及代碼示例
- Python PySpark DataFrame foreach方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
- Python Pandas DataFrame max方法用法及代碼示例
- Python Pandas DataFrame swaplevel方法用法及代碼示例
- Python Pandas DataFrame agg方法用法及代碼示例
- Python Pandas DataFrame copy方法用法及代碼示例
- Python Pandas DataFrame pow方法用法及代碼示例
- Python Pandas DataFrame insert方法用法及代碼示例
- Python Pandas DataFrame lt方法用法及代碼示例
- Python Pandas DataFrame all方法用法及代碼示例
- Python Pandas DataFrame unstack方法用法及代碼示例
- Python Pandas DataFrame mean方法用法及代碼示例
- Python Pandas DataFrame tz_convert方法用法及代碼示例
- Python Pandas DataFrame isin方法用法及代碼示例
- Python PySpark DataFrame collect方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | filter method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
