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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
