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)