0

I've written the following simple function, but it's running rather slow. For df1 I'm using a dataframe without any column (only index) and for df2 I'm using a dataframe with 10 columns and trying to add one df2's columns along with it's value to df1 if the indices match.

def add_labels_column(df1, df2):
    for idx1 in df1.index:
        for idx2 in df2.index:
            if idx1 == idx2:
                df1['Finding Labels'] = df2['Finding Labels']
    return df1

I'm looking for a faster solution, perhaps using pandas and/or numpy. I'm a new to Python, pandas or numpy.

Taivna
  • 15
  • 4
  • Use [Minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to post your code so that others can reproduce your problem and help solving. – theProcrastinator Mar 18 '21 at 17:32
  • 1
    There are several options, check [this](https://stackoverflow.com/a/65167356/11380795) thread. – RJ Adriaansen Mar 18 '21 at 17:34
  • @RJAdriaansen Thank you!, the merge function was exactly what I needed!, now I feel silly for not being able to figure out something like this.. – Taivna Mar 18 '21 at 21:58

1 Answers1

0

It's hard to answer your question without sample data, but one possible option is:

df1['Finding Labels'] = df1['Finding Labels'].update(df2['Finding Labels'])
ashkangh
  • 1,594
  • 1
  • 6
  • 9