Filtering pandas dataframe rows by contains str

前端 未结 1 1617
旧巷少年郎
旧巷少年郎 2020-12-03 03:07

I have a python pandas dataframe df with a lot of rows. From those rows, I want to slice out and only use the rows that contain the word \'ball\' in the \'body

相关标签:
1条回答
  • 2020-12-03 03:34

    You could either use .str again to get access to the string methods, or (better, IMHO) use case=False to guarantee case insensitivity:

    >>> df = pd.DataFrame({"body": ["ball", "red BALL", "round sphere"]})
    >>> df[df["body"].str.contains("ball")]
       body
    0  ball
    >>> df[df["body"].str.lower().str.contains("ball")]
           body
    0      ball
    1  red BALL
    >>> df[df["body"].str.contains("ball", case=False)]
           body
    0      ball
    1  red BALL
    >>> df[df["body"].str.contains("ball", case=True)]
       body
    0  ball
    

    (Note that if you're going to be doing assignments, it's a better habit to use df.loc, to avoid the dreaded SettingWithCopyWarning, but if we're just selecting here it doesn't matter.)

    (Note #2: guess I really didn't need to specify 'round' there..)

    0 讨论(0)
提交回复
热议问题