0

I have three dataframes:

df_total
df1
df2

Where

df2 = pd.merge(df_total, df1[[Portfolio, Value]], on=Portfolio, how = inner)

I merge df_total and df1 when the values of Portfolio column are the same. This results in df2.

How can I get a df3= that represents the rest of df_total that is not included in the intersection df2?

eduardo2111
  • 379
  • 3
  • 21
  • 2
    Does this answer your question? [pandas get rows which are NOT in other dataframe](https://stackoverflow.com/questions/28901683/pandas-get-rows-which-are-not-in-other-dataframe) – Daniel Geffen Jul 09 '20 at 18:47
  • No, because when I apply the solution, for example, df_total_rows are not equal to =df2+df3 (rows) – eduardo2111 Jul 09 '20 at 18:56

1 Answers1

0

I found the answer doing:

df2 = pd.merge(df_total, df1, how='outer', indicator=True)

This returns a dataframe like:

      col1  col2   col3     _merge
0    ...     ...    ...   left_only
1    ...     ...    ...        both
2    ...     ...    ...  right_only

And because I am only interested in "df3= that represents the rest of df_total that is not included in the intersection df2"

df2 = df2[df2['_merge'] == "both"].drop('_merge', 1)
df3 = df2[df2['_merge'] == "left_only"].drop('_merge', 1)
eduardo2111
  • 379
  • 3
  • 21