1

So I need to filter out rows from one data frame using another dataframe as a condition for it.

df1:

system     code
AIII-01    423
CIII-04    123
LV-02      142

df2:

StatusMessage    Event
123              Gearbox warm up

So for this example I need to remove the rows that has the code 423 and 142.

How do I do that?

NLGenin
  • 45
  • 1
  • 5

1 Answers1

3

Plug and play script for you. If this doesn't work on your regular code, check to make sure you have the same types in the same columns.

import pandas as pd

df1 = pd.DataFrame(
    {"system": ["AIII", "CIII", "LV"], "Code": [423, 123, 142]}
)

df2  = pd.DataFrame(
    {"StatusMessage": [123], "Event": ["Gearbox warm up"]}
)

### This is what you need
df1 = df1[df1.Code.isin(df2.StatusMessage.unique())]

print(df1)

zerecees
  • 697
  • 4
  • 13
  • Anurag's code will also work (and should be used if you want merging). You could also drop the `.unique()` in my code, depending on spacetime requirements. – zerecees Apr 09 '21 at 14:49
  • See my edit! Should help. If not, message me back. – zerecees Apr 09 '21 at 14:59