Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparison of two columns

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading