简体   繁体   中英

How can I combine the dataframes returned by a function efficiently in python?

I have a function which returns many values on every run but I am interested in dataframe being returned. I am currently using a list to store dataframes and then at the end I use pd.concat to convert all the dfs in to a single df. It's working fine but when I try to push this code for review I face linting errors "too many local variables'. There's no way I can deal with other varibales so, is there a way more efficient to store these dataframes and then later convert it to a single frame.

Currently, the code is something like this:

df_list = []

....

_, _, df = function_call()

df_list.append(df)

....

df = pd.concat(df_list, axis=0)

Would it be possible to store the argument values to pass to the dataframe generating function in a list, and then use a generator expression in the pd.concat call?

Something like this:

kwargs_list = []

kwargs = {'arg1': val1, 'arg2': val2}
kwargs_list.append(kwargs)
...

df = pd.concat(function_call(**kwargs) for kwargs in kwargs_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM