rosstripi
rosstripi

Reputation: 584

pandas: Add row from one data frame to another data frame?

I have two dataframes with identical column headers.

I am iterating over the rows in df1, splitting one of the columns, and then using those split columns to create multiple rows to add to the other dataframe.

for index, row in df1.iterrows():
    curr_awards = row['AWARD'].split(" ")
    for award in curr_awards:
        new_line = row
        new_line['AWARD'] = award.strip()
        #insert code here to add new_line to df2

What is the pandas method for adding a row to another data frame?

Upvotes: 3

Views: 6287

Answers (1)

rosstripi
rosstripi

Reputation: 584

I figured it out.

for index, row in df1.iterrows():
    curr_awards = row['AWARD'].split(" ")
    for award in curr_awards:
        new_line = row
        new_line['AWARD'] = award.strip()
        df2.loc[len(df2)] = new_line

Upvotes: 4

Related Questions