I have two DataFrames that have the same column names with some matching data and some unique data.
I want to exclude the middle and only save what is unique to both DataFrames.
How would I concat or merge or join these two dataframes to do so?
For instance in this image I do not want the middle in this image, I want both sides but not the middle:
Here's my code right now:
def query_to_df(query):
...
df_a = pd.DataFrame(data_a)
df_b = pd.DataFrame(data_b)
outer_results = pd.concat([df_a, df_b], axis=1, join='outer')
return df
Let me give you an example of what I need:
df_a =
col_a col_b col_c
a1 b1 c1
a2 b2 c2
df_b =
col_a col_b col_c
a2 b2 c2
a3 b3 c3
# they only share the 2nd row: a2 b2 c2
# so the outer result should be:
col_a col_b col_c col_a col_b col_c
a1 b1 c1 NA NA NA
NA NA NA a3 b3 c3
or I'd be just as happy with 2 dataframes
result_1 =
col_a col_b col_c
a1 b1 c1
result_2 =
col_a col_b col_c
a3 b3 c3
Lastly, you'll notice that a2 b2 c2
were excluded because all of the columns match - how do I specify that I want to join based on all the columns, not just 1? If df_a
had had a2 foo c2
I would have wanted that row to be in result_1
as well.