1

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?

0

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

with list I have ` 'NoneType' object has no attribute 'append' ` error
Hmmmm, db_df = pd.read_sql_query(sql, con) return for you DataFrame? Can you check it?
yes it is a dataframe, I think I do the wrong thing in drop duplicates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.