pytthon read DF1, DF2 make new DF12 where it contains rows from DF2 not containing pairs…
- User
pytthon
read DF1, DF2
make new DF12 where it contains rows from DF2 not containing pairs ['id'] ['text'] from DF1
- Assistant
To create a new DataFrame
DF12that contains rows fromDF2which do not have pairs of['id']and['text']present inDF1, you can use a combination of pandas operations: merging and filtering. Here's how you can do it in Python using the pandas library:pythonimport pandas as pd # Example DataFrames # DF1 DF1 = pd.DataFrame({ 'id': [1, 2, 3], 'text': ['foo', 'bar', 'baz'] }) # DF2 DF2 = pd.DataFrame({ 'id': [2, 3, 4, 5], 'text': ['bar', 'baz', 'qux', 'quux'] }) # Merge DF2 with DF1 on ['id', 'text'] with an indicator merged = DF2.merge(DF1, on=['id', 'text'], how='left', indicator=True) # Filter rows where the indicator is 'left_only', meaning present only in DF2 DF12 = merged[merged['_merge'] == 'left_only'].drop(columns='_merge') print(DF12)