I have two pandas dataframes:
import pandas as pd
df_1 = pd.DataFrame({'ID': [1, 2, 4, 7, 30],
'Instrument': ['temp', 'temp_sensor', 'temp_sensor',
'sensor', 'sensor'],
'Value': [1000, 0, 1000, 0, 1000]})
print(df_1)
ID Instrument Value
1 temp 1000
2 temp_sensor 0
4 temp_sensor 1000
7 sensor 0
30 sensor 1000
df_2 = pd.DataFrame({'ID': [1, 30],
'Instrument': ['temp', 'sensor'],
'Value': [1000, 1000]})
print(df_2)
ID Instrument Value
1 temp 1000
30 sensor 1000
I need to exclude from df_1 the lines that also exist in df_2. So I made the code:
combined = df_1.append(df_2)
combined[~combined.index.duplicated(keep=False)]
The (wrong) output is:
ID Instrument Value
4 temp_sensor 1000
7 sensor 0
30 sensor 1000
I would like the output to be:
ID Instrument Value
2 temp_sensor 0
4 temp_sensor 1000
7 sensor 0
I relied on what was explained in: How to remove a pandas dataframe from another dataframe