0

I have two different DataFrames and i wanna get a new DataFrame with the indexes that are different.

DataFrame A:

Name Color
Jony Blue
Mike Red
Joanna Green

DataFrame B:

Name Color
Jony Blue
Mike Red

DataFrame Output:

Name Color
Joanna Green

How can i do to get this DataFrame Output?

4 Answers 4

1

Using drop_duplicates

import pandas as pd
dataA = {'Name':['Jony', 'Mike', 'Joanna'], 'Color':['Blue', 'Red', 'Green']}
dataB = {'Name':['Jony', 'Mike'], 'Color':['Blue', 'Red']}

dfA = pd.DataFrame(dataA)
dfB = pd.DataFrame(dataB)

df = pd.concat([dfA, dfB]).drop_duplicates(keep=False, ignore_index=True)
Sign up to request clarification or add additional context in comments.

3 Comments

I thought about this method, but realized the author specifically asked about matching Indexes, not fully matching rows~
You can limit drop_duplicates to the subset you want. The only drawback of this approach is if you have duplicates unique to one of the dataframe and want to keep them in the output.
How would you select the index as the subset for drop_duplicates?
1

Maybe using 'symmetric difference' on a set that you convert to a Series of df ?

dfc=pd.DataFrame()

dfc['Name Color']=pd.Series(list(set(dfa['Name Color']).symmetric_difference(set(dfb['Name Color']))))

Comments

1

One option is to outer-merge with the indicator parameter set to True. Then the common rows will be flagged "both" and since you don't want the common rows, you filter them out:

out = df1.merge(df2, how='outer', indicator=True).query('_merge!="both"').drop(columns='_merge')

Output:

     Name  Color
2  Joanna  Green

Comments

1

Assuming the Name Column is the index in both dataframes:

df_a = pd.DataFrame({'Color': {'Jony': 'Blue', 'Mike': 'Red', 'Joanna': 'Green'}})
df_a = df_a.rename_axis('Name')
df_b = pd.DataFrame({'Color': {'Jony': 'Blue', 'Mike': 'Red'}})
df_b = df_b.rename_axis('Name')

df = pd.concat([df_a[~df_a.index.isin(df_b.index)], df_b[~df_b.index.isin(df_a.index)]])
print(df)

Output:

        Color
Name         
Joanna  Green

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.