Home > Software engineering >  Comparison of two columns
Comparison of two columns

Time:10-12

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

CodePudding user response:

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

CodePudding user response:

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
  • Related