You should accept one of the 5 answers that have been posted, there will be a tick mark underneath the voting buttons, note that the str.contains answers are probably the fastest and recommended method for your requirements: pandas.pydata.org/pandas-docs/stable/generated/… – EdChumJan 16 '15 at 9:00
If it was a specific word, to negate, could you also use: df = df[df.id != "ball"] – BrianApr 26 '17 at 17:59
@Brian - Yes, in the above df you can try df = df[df.ids != "aball"] to see it in action. – Amit VermaApr 27 '17 at 2:03
@Amit: I need to access columns by id instead of name. However trying str gives me an error [AttributeError: 'DataFrame' object has no attribute 'str'] Does new pandas not support it or is it because of number based access? – Sameer MahajanOct 23 '17 at 10:01
df[df['ids'].str.contains('ball', na =False)]# valid for (at least) pandas version 0.17.1
Step-by-step explanation (from inner to outer):
df['ids'] selects the ids column of the data frame (technically, the object df['ids'] is of type pandas.Series)
df['ids'].str allows us to apply vectorized string methods (e.g., lower, contains) to the Series
df['ids'].str.contains('ball') checks each element of the Series as to whether the element value has the string 'ball' as a substring. The result is a Series of Booleans indicating True or False about the existence of a 'ball' substring.
df[df['ids'].str.contains('ball')] applies the Boolean 'mask' to the dataframe and returns a view containing appropriate records.
na = False removes NA / NaN values from consideration; otherwise a ValueError may be returned.
str.contains
answers are probably the fastest and recommended method for your requirements: pandas.pydata.org/pandas-docs/stable/generated/… – EdChum Jan 16 '15 at 9:00