Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
duplicates
#1
Hello,

I have an assingment, and what seems a simple question, that is driving me nuts and would like a point in the right direction (not an answer) if possible.

I have been given two seperate dataframes that contain sales data. Both have a 'country' column that gives the names of various contries where each sale originated from.

My task is simply to return a list (or set, dataframe, series) of values that are found in the 'country' column of the two dataframes.

So if I had for example the following two sets of values in these columns....
df1['column1'] = ['England','Germany','Netherlands','Scotland','Spain']
df2['column2'] = ['England','Scotland','Wales,'Spain','Italy','France']
I would want to return a list that contains just England, Scotland and Spain as they are the only values that appear in both dataframes.

So far I have extracted the unique values of both columns and placed them in their own dataframe - so a new dataframe with just the two columns (the unique values from the first df and then the unique values from the 2nd df).

My problem is that trying to use either the duplicate(), Merge() or intersection methods are not working for me. The duplicate() method does not work at all and the merge and intersection methods do not return enough matches.

And i think it is because the two columns of my new dataframe have different length in terms of the rows. The frist column has 300 rows whilst the 2nd column only has 210. So the merge and interesection methods I beleive are trying to match on the values, but also on the indexes too.

I need to return a list irrelevant of indexes and just find values that are the same in both columns.

Any pointers please without giving away answers?

Thanks
Reply
#2
You can do with your dataframes something like this, in lists
lst1 = ['England','Germany','Netherlands','Scotland','Spain']
lst2 = ['England','Scotland','Wales','Spain','Italy','France']
lst3 = []
for country in lst1 :
    if country in lst2 :
        lst3.append(country)
print(lst3)
Reply
#3
Convert both lists of country names into sets and get the intersection.
Reply
#4
Thanks both.

I have the intersection working once cast to sets. All is now fine and returning a list of the correct matches.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 1,970 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,196 Oct-28-2020, 05:16 PM
Last Post: Askic
  Exercise list remove duplicates RavCOder 9 5,198 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  how to take out duplicates from a list? helpme 2 2,463 Mar-08-2019, 02:27 PM
Last Post: ichabod801
  How to keep duplicates and remove all other items in list? student8 1 4,914 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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