1

I am trying to do something really simple, but I couldn't find a solution, yet:

Given a dataframe df I am trying to select a column (with strings) and drop items that are named "x".

I have found questions about removing by index (Drop Elements from Pandas Series by Index) but not by their content.

s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])

Trying to remove:

s.drop('A')
s.drop(s['A'])

How can I achieve that?

lukas_o
  • 3,776
  • 4
  • 34
  • 50
  • Thanks for finding the other questions! I saw the first one (filter a Series), but I couldn't really abstract the way how to do that with strings (my bad, new to python). I think the second one (Select rows...) is not a duplicate, since it is about selection only. – lukas_o Mar 30 '18 at 13:27

1 Answers1

2

For a series as you have, try just indexing like this:

s.loc[s!='A']

For a dataframe with named columns, try:

s.loc[s['column_name'] != 'A']
sacuL
  • 49,704
  • 8
  • 81
  • 106