Skip to content Skip to sidebar Skip to footer

Replace Some Values In A Dataframe With Nan's If The Index Of The Row Does Not Exist In Another Dataframe

I have a really large dataframe similar to this: CustomerId Latitude Longitude 0. a x1 y1 1. a x2 y2 2. b x3

Solution 1:

First we create a mask with pandas.DataFrame.isin

After that we use np.where and ask for the opposite with ~

mask = df.CustomerId.isin(df2.CustomerId)

df['Latitude']  = np.where(~mask, np.NaN, df['Latitude'])
df['Longitude'] = np.where(~mask, np.NaN, df['Longitude'])

print(df)
    CustomerId Latitude Longitude
0.0          a       x1        y1
1.0          a       x2        y2
2.0          b      NaN       NaN
3.0          c       x4        y4

Explanation: np.where works as following: np.where(condition, value if true, value if false)

Post a Comment for "Replace Some Values In A Dataframe With Nan's If The Index Of The Row Does Not Exist In Another Dataframe"