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