0

Data

I have a dataframe in the left side (in orange) with the LeadOrderIDs. I want to add a column to the left to see whether the list of OrderId in the right (purple) is in the LeadOrder (Orange). Don't use the inner join...

How should I use python - Pandas to do this?

Thanks for your help.

Boomshakalaka
  • 521
  • 1
  • 6
  • 19
  • Please post dataframes as text, not as images. Also do you want *anywhere* in the left side, or in the same row on the left side – user3483203 Sep 17 '18 at 22:21

1 Answers1

0

You can use the .isin method. Here is an example how it would work. The data frame is:

df=pd.DataFrame({'LeadOrder':[1,2,3,4, 5,6,7,8,9,10], 'OrderId':[1, 3, 5, 7, 9, 0, 0, 0, 0, 0]})

The common elements are given by the following mask:

mask = df['LeadOrder'].isin(df['OrderId'])

And if you want to get the common elements just put this mask into the index place when subsetting the data frame:

df.loc[mask,]

In your case you will have to complete the OrderId column with NAs or some other irrelevant values that are not in the LeadOrder column for sure.

Zsolt Diveki
  • 159
  • 8