I have a list which consists of a different colours, all stored as string variables.
Preferredcolours = ['red','yellow','green', 'blue']
I have a panda array, which contains information about cars. One of the column DfCar['colour'] consists of the colours of these cars. I want to create a new variable in my data frame, column named PreferredMathcing which =1 if the DataFrame colour column matches with one of the list colours. How can I use a for loop to solve this?
I would ideally want this sort of a solution:
+=================+============================+
| DfCar['colour'] | DfCar['PreferredMathcing'] |
+=================+============================+
| white | 0 |
+-----------------+----------------------------+
| yellow | 1 |
+-----------------+----------------------------+
| black | 0 |
+-----------------+----------------------------+
| purple | 0 |
+-----------------+----------------------------+
| green | 1 |
+-----------------+----------------------------+
df['PreferredMatching'] = df[df.colour.isin(PreferredColours)].astpye(int)apply(...)for simple stuff can take x20 - x30 times more then a dedicated function. that is - half an hour instead of 1m, or a full day instead of 1h...