Python Forum
Remove if similar values available based on two columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove if similar values available based on two columns
#1
I want to remove the duplicates (keeping only the first occurrence) if similar values are found between two columns.

import pandas as pd
df1 = pd.DataFrame({'Customer':['John Tom', 'Harry', 'Simon', 'Tom'],
                    'Supplier':['Harry', 'John Tom', 'Harry', 'Simon']})
ie. I want to remove the second-row values as both values in that row were found previously in two columns in one row.

My expected result
Output:
Customer Supplier 0 John Tom Harry 1 Simon Harry 2 Tom Simon
Is there a way to remove such occurrences by considering them as duplicates?

appreciate it if someone can help
Reply
#2
import pandas as pd

df1 = pd.DataFrame({'Customer':['John Tom', 'Harry', 'Simon', 'Tom'],
    'Supplier':['Harry', 'John Tom', 'Harry', 'Simon']})

for idx in df1:
    eseries = pd.Series(df1[idx])
    print(f"df1[{idx}] unique = {eseries.unique()}")
results:
Output:
df1[Customer] unique = ['John Tom' 'Harry' 'Simon' 'Tom'] df1[Supplier] unique = ['Harry' 'John Tom' 'Simon']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove some columns James_S 4 728 Dec-16-2023, 11:02 PM
Last Post: James_S
  Remove values for weekend in a panda series JaneTan 0 647 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,037 Jan-09-2022, 02:39 AM
Last Post: Python84
  Calculate next rows based on previous values of array divon 0 1,716 Nov-23-2021, 04:44 AM
Last Post: divon
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,545 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  How to remove a column or two columns in a correlation heatmap? lulu43366 3 5,082 Sep-30-2021, 03:47 PM
Last Post: lulu43366
  Remove Specific Columns when the number of columns is greater than a specific value CuriousOne 0 1,272 Sep-09-2021, 09:17 PM
Last Post: CuriousOne
  How to covert row values into columns values? sankarachari 8 3,198 Aug-06-2021, 10:14 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020