- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.filter#
- DataFrame.filter(items=None,like=None,regex=None,axis=None)[source]#
Subset the dataframe rows or columns according to the specified index labels.
Note that this routine does not filter a dataframe on itscontents. The filter is applied to the labels of the index.
- Parameters:
- itemslist-like
Keep labels from axis which are in items.
- likestr
Keep labels from axis for which “like in label == True”.
- regexstr (regular expression)
Keep labels from axis for which re.search(regex, label) == True.
- axis{0 or ‘index’, 1 or ‘columns’, None}, default None
The axis to filter on, expressed either as an index (int)or axis name (str). By default this is the info axis, ‘columns’ forDataFrame. ForSeries this parameter is unused and defaults toNone.
- Returns:
- same type as input object
See also
DataFrame.loc
Access a group of rows and columns by label(s) or a boolean array.
Notes
The
items
,like
, andregex
parameters areenforced to be mutually exclusive.axis
defaults to the info axis that is used when indexingwith[]
.Examples
>>>df=pd.DataFrame(np.array(([1,2,3],[4,5,6])),...index=['mouse','rabbit'],...columns=['one','two','three'])>>>df one two threemouse 1 2 3rabbit 4 5 6
>>># select columns by name>>>df.filter(items=['one','three']) one threemouse 1 3rabbit 4 6
>>># select columns by regular expression>>>df.filter(regex='e$',axis=1) one threemouse 1 3rabbit 4 6
>>># select rows containing 'bbi'>>>df.filter(like='bbi',axis=0) one two threerabbit 4 5 6