0

I have two populated DataFrames, df1 and df2. I also have an empty Dataframe (test):

df1 = pd.read_excel(xlpath1, sheetname='Sheet1')
df2 = pd.read_excel(xlpath2, sheetname='Sheet1')
test = pd.DataFrame()

I'd like to iterate through the rows of df1 and add those rows to the empty test Dataframe. When I try the following, I don't get any sort of error, but nothing is added to the test DataFrame:

for i, j in df1.iterrows():
    test.append(j)

Any ideas? Do I need to add the proper columns to the test DataFrame first? My total end-goal is to iterate through multiple DataFrames and add only unique items to the empty DataFrame (ex, adding items that appear in one of the many DataFrames).

Jared
  • 117
  • 4
  • 1
    You should avoid both iterating over the rows and [appending to a DataFrame within a loop](https://stackoverflow.com/a/37009561/4333359). Can you provide some sample DataFrames and you expected output? Likely just concatenate them and then drop duplicates. – ALollz Jun 11 '19 at 15:59

1 Answers1

1

If are trying to append dataframe df1 on empty dataframe df2 you can use concat function of pandas.

test = pd.concat([df1, test], axis = 0)

axis = 0 ; for appending two dataframes row-wise

Shubham Rajput
  • 192
  • 3
  • 12