@@ -4640,6 +4640,90 @@ def _reindex_with_indexers(
4640
4640
4641
4641
return self ._constructor (new_data ).__finalize__ (self )
4642
4642
4643
+ def select_str (
4644
+ self , * , startswith = None , endswith = None , regex = None , flags = 0 , axis = None
4645
+ ):
4646
+ """
4647
+ Selects rows or columns of dataframe according to string labels in
4648
+ the specified index.
4649
+
4650
+ Only one of keywords arguments `startswith`, `endswith` and `regex` can be used.
4651
+
4652
+ Parameters
4653
+ ----------
4654
+ startswith: str, optional
4655
+ Test if the start of each string element matches a pattern.
4656
+ Equivalent to :meth:`str.startswith`.
4657
+ endswith: str, optional
4658
+ Test if the end of each string element matches a pattern.
4659
+ Equivalent to :meth:`str.endsswith`.
4660
+ regex : str, optional
4661
+ Keep labels from axis for which re.search(regex, label) is True.
4662
+ flags : int, default 0 (no flags)
4663
+ re module flags, e.g. re.IGNORECASE. Can only be used with parameter regex.
4664
+ axis : int or string axis name
4665
+ The axis to filter on. By default this is the info axis,
4666
+ 'index' for Series, 'columns' for DataFrame.
4667
+
4668
+ Returns
4669
+ -------
4670
+ same type as input object
4671
+
4672
+ See Also
4673
+ --------
4674
+ DataFrame.loc
4675
+ DataFrame.select_dtypes
4676
+
4677
+ Examples
4678
+ --------
4679
+ >>> df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6])),
4680
+ ... index=['mouse', 'rabbit'],
4681
+ ... columns=['one', 'two', 'three'])
4682
+
4683
+ >>> df.select_str(startswith='t')
4684
+ two three
4685
+ mouse 2 3
4686
+ rabbit 5 6
4687
+
4688
+ >>> # select columns by regular expression
4689
+ >>> df.select_str(regex=r'e$', axis=1)
4690
+ one three
4691
+ mouse 1 3
4692
+ rabbit 4 6
4693
+
4694
+ >>> # select rows containing 'bbi'
4695
+ >>> df.select_str(regex=r'bbi', axis=0)
4696
+ one two three
4697
+ rabbit 4 5 6
4698
+ """
4699
+ import re
4700
+
4701
+ num_kw = com .count_not_none (startswith , endswith , regex )
4702
+ if num_kw != 1 :
4703
+ raise TypeError (
4704
+ "Only one of keywords arguments `startswith`, `endswith` and "
4705
+ "`regex` can be used."
4706
+ )
4707
+ if regex is None and flags != 0 :
4708
+ raise ValueError ("Can only be used togehter with parameter 'regex'" )
4709
+
4710
+ if axis is None :
4711
+ axis = self ._info_axis_name
4712
+ labels = self ._get_axis (axis )
4713
+
4714
+ if startswith is not None :
4715
+ mapped = labels .str .startswith (startswith )
4716
+ elif endswith is not None :
4717
+ mapped = labels .str .endsswith (endswith )
4718
+ else : # regex
4719
+ matcher = re .compile (regex , flags = flags )
4720
+
4721
+ def f (x ):
4722
+ return matcher .search (x ) is not None
4723
+
4724
+ mapped = labels .map (f )
4725
+ return self .loc (axis = axis )[mapped ]
4726
+
4643
4727
def filter (self , items = None , like = None , regex = None , axis = None ):
4644
4728
"""
4645
4729
Subset rows or columns of dataframe according to labels in
0 commit comments