Python Forum
Comparing Items Different Data Frames With a WHILE Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing Items Different Data Frames With a WHILE Loop
#1
Hello all

I was wondering if anyone could help with comparing items in one data frame with an items in another data frame.

I have 2 x data frames
DF_One
DF_TWO

I want to loop through all of the values in DF_One in the first column and compare this with all of the values in the first column in DF_Two.

The Code would be like:-

Do until the end of DF_One is reached
...Current_Value = value in row (t),column 1 of DF_One
......Do until the end of DF_Two is reached
.........if Current_Value = row(i), Column 1 in DF_Two
............print ("found")
.........end if
......i=i+1
t=t+1

I want to use a while loop and not the where function.

Can anyone help or just point me in the right direction.

Thank you.
Reply
#2
There is .isin method, which provides this functionality.
import pandas as pd
df1 = pd.DataFrame({'x': pd.np.random.randint(1, 1000, size=200)})
df2 = pd.DataFrame({'x': pd.np.random.randint(1, 1000, size=100)}) 
df1.x.isin(df2.x)
Pure Python loops will be very slow in case of large dfs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,103 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Is there a more elegant way to concatenate data frames? db042190 3 919 Jun-13-2023, 05:08 PM
Last Post: snippsat
  How to map two data frames based on multiple condition SriRajesh 0 1,474 Oct-27-2021, 02:43 PM
Last Post: SriRajesh
  Comparing items from 2 lists of dictionaries illwill 7 2,753 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Moving Rows From Different Data Frames JoeDainton123 1 4,334 Aug-06-2020, 05:19 AM
Last Post: scidam
  merging data frames sportcardinal 0 1,203 Jun-30-2020, 12:21 AM
Last Post: sportcardinal
  Merge CSV Column using Pandas Data Frames davidlang 1 2,606 May-01-2020, 02:43 PM
Last Post: klllmmm
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 2,745 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  Comparing data from two Excel sheets Stuart_Houghton 0 1,683 Jan-19-2020, 02:49 PM
Last Post: Stuart_Houghton
  removing items from a list or group within a for loop. allusernametaken 9 4,583 Nov-13-2019, 01:58 AM
Last Post: allusernametaken

Forum Jump:

User Panel Messages

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