Search code examples
pythonpandasindexinglocationrow

how to loop through the same indexed row and column number from two separate Pandas dataframes


I have two separate Pandas Dataframes with the same dimensions.

df1

column1 column2 column3
1 "A" "B" "C"
2 "A" "B" "C"
3 "A" "B" "C"

df2

column1 column2 column3
1 "E" "A" "C"
2 "E" "F" "G"
3 "H" "I" "J"

How can I loop through all df1 row 1 and search for: "A". If "A" is found then take the row and column index and look in df2 for what is in that cell. In this case it would be "E".

I have tried:

for row in df1:
   if row.columns contains "A":
       col_marker = column index
       search df2 @ col_marker for whats in that cell...

   else: 
       continue

I am expecting the for loop to search each row of df1, find "A" then take that location and search df2 at the same location.


Solution

  • The iterrows() method is used to loop through the rows of df1. For each row, the row index is used to access the same row in df2 with the loc method.

    # Loop through the rows of df1
    for index, row in df1.iterrows():
        # Check if "A" is in the row
        if "A" in row.values:
            # Get the value in the same row and column in df2
            value = df2.loc[index, "column1"]
            print(value)
    
    E
    E
    H
    

    You can also use df2[df1 == "A"] if you prefer the following output.

    index column1 column2 column3
    0 E NaN NaN
    1 E NaN NaN
    2 H NaN NaN