Given a data frame df:
Column A: [0, 1, 3, 4, 6]
Column B: [0, 0, 0, 0, 0]
The goal is to conditionally replace values in column B. If column A's values exist in a set assginedToA, we replace the corresponding values in column B with a constant b.
For example: if b=1 and assignedToA={1,4}, the result would be
Column A: [0, 1, 3, 4, 6]
Column B: [0, 1, 0, 1, 0]
My code for finding the A values and write B values into it looks like this:
df.loc[df['A'].isin(assignedToA),'B']=b
This code works, but it is really slow for a huge dataframe. Do you have any advice, how to speed this process up?
The dataframe df has around 5 Million rows and assignedToA has a maximum of 7 values.