0

I have a Dataframe df:

name     rank
A    captain, general, soldier
B    general, foo, major
C    foo
D    captain, major
E    foo, foo, foo

I want to check if any cell in the column rank consists of foo and if it does replace the whole cell with foo.

Expected output:

name     rank
A    captain, general, soldier
B    foo
C    foo
D    captain, major
E    foo

How can I do this?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Yantra Logistics
  • 198
  • 4
  • 19
  • Does this answer your question? [Check if string is in a pandas dataframe](https://stackoverflow.com/questions/30944577/check-if-string-is-in-a-pandas-dataframe) – Pranav Hosangadi Jun 20 '22 at 05:31
  • Does this answer your question? [Conditional Replace Pandas](https://stackoverflow.com/questions/21608228/conditional-replace-pandas) – Tzane Jun 20 '22 at 06:12

4 Answers4

1

You can apply a lambda function to the column :

df["rank"] = df["rank"].apply(lambda x: "foo" if "foo" in x.split(", ") else x)

Splitting on the separator allows to check for words. For example, the world "foobar" wouldn't trigger the transformation on its row.

Edit: Thanks to BeRT2me for suggesting to split by ', '.

Whole Brain
  • 2,097
  • 2
  • 8
  • 18
1
df['rank'].replace('.*foo.*', 'foo', regex=True, inplace=True)
# OR
df['rank'].mask(df['rank'].str.contains('foo'), 'foo', inplace=True)
# OR
df.loc[df['rank'].str.contains('foo'), 'rank'] = 'foo'

Output:

  name                       rank
0    A  captain, general, soldier
1    B                        foo
2    C                        foo
3    D             captain, major
4    E                        foo
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
0
mask = df['rank'].str.contains('foo')
df.loc[mask, 'rank'] = 'foo'
Smaurya
  • 167
  • 9
-1
if df['rank'].str.contains('foo').any():
 df['rank']='foo'
new2cod3
  • 31
  • 5