How to apply string methods to multiple columns of a dataframe

前端 未结 3 423
生来不讨喜
生来不讨喜 2021-02-06 02:05

I have a dataframe with multiple string columns. I want to use a string method that is valid for a series on multiple columns of the dataframe. Something like this is what I w

3条回答
  •  有刺的猬
    2021-02-06 02:20

    You can mimic the behavior of rstrip using replace with regex=True, which can be applied to the entire DataFrame:

    df.replace(r'f$', '', regex=True)
    

         A    B
    0  123  789
    1  456  901
    

    Since rstrip takes a sequence of characters to strip, you can easily extend this:

    df.replace(r'[abc]+$', '', regex=True)
    

提交回复
热议问题