Pandas iterate over rows and remove string value in one column from another

Harvey :

I have dataframe (carsML) that looks something like this:

+-----------------+----------+--------------+
| carManufacturer | carModel |   carType    |
+-----------------+----------+--------------+
| VW              | POLO     | 1.4 TDI      |
| VW              | POLO     | POLO 1.4 TDI |
| VW              | POLO     | 1.6 TDI      |
| VW              | POLO     | 1.4          |
| VW              | POLO     | POLO 1.6 TDI |
|+-----------------+----------+--------------+

I want to iterate over rows, check weather carModel is contained in carType and if it is than remove it. So instead of having POLO 1.4 TDI it should be just 1.4 TDI.

One constraint - some carModels can be single letter long (like just 1 or A). In that case skip replacement and do nothing. Script should work only for carModels that are len(carModel)>1

So far I have:

for row in carsML.itertuples():
    if len(row.carModel) > 1:
        carsML.iloc[row.Index].carType = row.carType.replace(row.carModel,"")

But this doesn't changes anything..I don't know why..

Jaroslav Bezděk :

If I understand you well, the following one-liner could do your job:

carsML.carType = carsML.apply(lambda row: row.carType.strip(row.carModel) if len(row.carModel) > 1 else row.carType, axis=1)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=3983&siteId=1