Home > other >  How can I conditionally replace all values in a column depending on cell content?
How can I conditionally replace all values in a column depending on cell content?

Time:09-02

I have a dataframe and I would like to replace all the cells that contain the string "low" with just "low". Could anyone help me please? Below is my dataframe.

I have tried to this with replace and loc:

df.loc[df['Stock'].str.contains('low').replace(to_replace=None,value="low",regex=True)]

however this gives me a regex error. I have tried reading the documentation for replace too, but havent gotten much further as it wouldnt be an exact match that I would be replacing.

I have tried it by getting a list about what contains "low" but that hasnt been working either:

list(df['Stock'])
['low stock', 'low stck', 'low stock', 'in stock', 'in stock', 'instock', 'lowstock', 'low', 'low on stock', 'in stock', 'in stock a lot', 'low_ on stock', 'low', 'low_on_stock ', ' lowstock', 'in stock', 'instock']

list(df.loc[df['Stock'].str.contains('low')])
['Company', 'Country', 'Stock']

list(df['Stock'].str.contains('low'))
[True, True, True, False, False, False, True, True, True, False, False, True, True, True, True, False, False]
        Company Country           Stock
0      Michelin     USA       low stock
1   Continental     USA        low stck
2   Continental     USA       low stock
3      Michelin     USA        in stock
4      Michelin     USA        in stock
5      Michelin     USA         instock
6      Michelin     USA        lowstock
7      Michelin     USA             low
8      Michelin     USA    low on stock
9       Pirelli     USA        in stock
10     Goodyear     USA  in stock a lot
11     Michelin     USA   low_ on stock
12  Continental     USA             low
13     Michelin     USA   low_on_stock 
14  Continental     USA        lowstock
15     Michelin     USA        in stock
16     Michelin     USA         instock

CodePudding user response:

this should work:

df.loc[df["Stock"].str.contains("low"), "Stock"] = "low"

CodePudding user response:

.str.contains is the correct method for this.

You can either assign the values directly (like grymlin shows), or use .mask() which is more flexible as it accepts the argument inplace (by default False) and can be chained:

df['Stock'].mask(df['Stock'].str.contains('low'), 'low')

CodePudding user response:

I'd use lambda with a condition for such operation

>>> df_stock
                 stock
0        low stock
1         low stck
2        low stock
3         in stock
4         in stock
5          instock
6         lowstock
7              low
8     low on stock
9         in stock
10  in stock a lot
11   low_ on stock
12             low
13   low_on_stock
14        lowstock
15        in stock
16         instock
>>> df_stock['stock'].apply(lambda x: 'low' if 'low' in x else x)
0                low
1                low
2                low
3           in stock
4           in stock
5            instock
6                low
7                low
8                low
9           in stock
10    in stock a lot
11               low
12               low
13               low
14               low
15          in stock
16           instock
Name: stock, dtype: object
  • Related