3

I am trying to combine two tables row wise (stack on top of each other, like using rbind in R). I've followed steps mentioned in:

Pandas version of rbind

how to combine two data frames in python pandas

But none of the "append" or "concat" are working for me.

About my data

I have two panda dataframe objects (type class 'pandas.core.frame.DataFrame'), both have 19 columns. when i print each dataframe they look fine.

The problem

So I created another panda dataframe using:

query_results = pd.DataFrame(columns=header_cols)

and then in a loop (because sometimes i may be combining more than just 2 tables) I am trying to combine all the tables:

for CCC in CCCList:
    query_results.append(cost_center_query(cccode=CCC))

where cost_center_query is a customized function and returns pandas dataframe objects with same column names as the query_results.

however, with this, whenever i print "query_results" i get empty dataframe.

any idea why this is happening? no error message as well, so i am just confused. Thank you so much for any advice!

8
  • 6
    .append returns a new data frame which you would need to assign to something, or else the result simply gets discarded. Commented Apr 10, 2017 at 20:19
  • After append store result into a variable and that will hold values you want. Commented Apr 10, 2017 at 20:20
  • Note, in your quoted question, the accepted answer has bigdata = data1.append(data2, ignore_index=True) Commented Apr 10, 2017 at 20:20
  • ah! thank you so much! that was quite a simple fix! feeling embarrassed to ask such beginner question but thank you all for your quick help! Commented Apr 10, 2017 at 20:21
  • 3
    Possible duplicate of Appending to an empty data frame in Pandas? Commented Apr 10, 2017 at 20:21

1 Answer 1

2

Consider the concat method on a list of dataframes which avoids object expansion inside a loop with multiple append calls. Even consider a list comprehension:

query_results = pd.concat([cost_center_query(cccode=CCC) for CCC in CCCList], ignore_index=True)
Sign up to request clarification or add additional context in comments.

Comments

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.