TheNone
TheNone

Reputation: 5802

Combine DataFrame in Pandas

I have to connect and read data from 10 databases and save data as pandas data frame. After combining dataframes, I have an empty dataframe:

df1 = pd.DataFrame(columns={'name', 'ip'})

# in a loop I connect to db and read sql data and combine data 

for db in database_list:
  db_df = pd.read_sql_query(sql, con)
  df1 = df1.append(db_df)
  df1 = df1.drop_duplicates(subset='name', keep='last')

print df1

df1 is an empty dataframe. What is the correct way for combine dataframes in this situation?

Upvotes: 1

Views: 202

Answers (1)

jezrael
jezrael

Reputation: 862581

I think you can append each DataFrame to list and last concat all to one big df:

dfs = []
for db in database_list:
  db_df = pd.read_sql_query(sql, con)
  print db_df
  db_df = db_df.drop_duplicates(subset='name', keep='last')
  dfs.append(db_df)

df1 = pd.concat(dfs, ignore_index=True)
print df1

Upvotes: 2

Related Questions