1

Lets say I have these functions:

def query():
    dict = (
             { "NO" : 1, "PART" : "ALPHA" }, 
             { "NO" : 2, "PART" : "BETA" }
           )
    finalqueryresult = pandas.DataFrame()

    for info in dict: #I use this loop to request query depends on the dict data, in this example twice (2 records from dict)
         finalqueryresult.append( sendquery(info["NO"], info["PART"]) )

def sendquery( no, part):
     *some code to request query to server and save it under reqresult variable*
     *.....*
     *.....*
     return reqresult

For example above, when sending first query (record with "NO" = 1) it will return: (lets say this is df1)

  NAME  COUNTRY
1  RYO   JPN
2  JON   NZ

and the last query (record with "NO" = 2): (lets say this df2)

   NAME    COUNTRY
1  TING      CN
2  ASHYU     INA

and what I want is finalqueryresult will be like this: (df1 combined with df2):

  NAME     COUNTRY
1 RYO        JPN
2 JON        NZ
3 TING       CN
4 ASHYU      INA

But I failed, the finalqueryresult is always empty. I suppose something is wrong with this:

for info in dict:
     finalqueryresult.append( sendquery(info["NO"], info["PART"]) )

1 Answer 1

2

I think you need first append all DataFrames to list dfs and then use concat:

dfs= []
for info in dict:
     #sendquery(info["NO"], info["PART"] return DataFrame
     dfs.append( sendquery(info["NO"], info["PART"]) )

finalqueryresult = pd.concat(dfs, ignore_index=True)  
Sign up to request clarification or add additional context in comments.

2 Comments

nice ! I didn't know about the ignore_index argument.
Glad can help you! Good luck!

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.