Comparison of two columns

Question:

How can I find the same values in the columns regardless of their position?

df = pd.DataFrame({'one':['A','B', 'C', 'D', 'E', np.nan, 'H'],
               'two':['B', 'E', 'C', np.nan, np.nan, 'H', 'L']})

The result I want to get:

    three
0   B
1   E
2   C
3   H
Asked By: Alex Brunno

||

Answers:

The exact logic is unclear, you can try:

out = pd.DataFrame({'three': sorted(set(df['one'].dropna())
                                   &set(df['two'].dropna()))})

output:

  three
0     B
1     C
2     E
3     H

Or maybe you want to keep the items of col two?

out = (df.loc[df['two'].isin(df['one'].dropna()), 'two']
         .to_frame(name='three')
      )

output:

  three
0     B
1     E
2     C
5     H
Answered By: mozway

Try this:

df = pd.DataFrame(set(df['one']).intersection(df['two']), columns=['Three']).dropna()
print(df)

Output:

  Three
1     C
2     H
3     E
4     B
Answered By: I'mahdi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.