# [SOLVED] Is there a more efficient way to use two for loops in python?

## Issue

I have heard that using two for loops within one another takes a long time. However, I’m not sure how to get around this issue. Each of my for loops are ~40,000 loops, so the time is very long. I am basically trying to compare entries of two DataFrames. If three of the entries (frame, x, and y) in a certain row match between the two, then I want to save that to a new array of data. My end result is to have a dataframe with frame, x, y, particle #, and two different intensities). I believe what I am doing will work, but it takes over 5 hours to get a result. Is there anyway I can make this code quicker and more efficient? Thank you so much, my code is posted below.

``````intensity = np.zeros((tracks.shape, 2))
location = np.zeros((tracks.shape, 2))
frame = np.zeros((tracks.shape, 1))
particle = np.zeros((tracks.shape, 1))

break
``````

## Solution

Use an inner merge to filter rows where `x`, `y` and `frame` match.

``````# example data
red = pd.DataFrame({"x": [1.2, 3.3, 5.4, 1], "y": [1.1, 10.7, 9.3, 1.1], "frame": ["a", "b", "c", "a"], "mass": [10, 20, 30, 40]})
green = pd.DataFrame({"x": [1.2, 2.3, 3.4, 1], "y": [1.1, 4.7, 6.3, 1.1], "frame": ["a", "b", "c", "a"], "mass": [50, 60, 70, 80]})

# Add rounded versions of x and y
for df in [red, green]:
df["xr"] = df["x"].round()
df["yr"] = df["y"].round()

matches = pd.merge(red, green, on=["xr", "yr", "frame"], suffixes=["_red", "_green"])
``````

There can in principle be multiple matches in red to a particular row in green, and vice versa. If so you will have to decide what to do with these, e.g. keep only the first of each group, using `matches = matches.groupby(["xr", "yr", "frame"]).agg("first")`.

You can work with the resulting dataframe directly, or convert it to numpy arrays:

``````intensity = matches[["mass_red", "mass_green"]].to_numpy()
location = matches[["x_red", "y_red"]].to_numpy()
frame = matches["frame_red"].to_numpy()
particle = matches["particle_red"].to_numpy()
``````