2

I have n dataframes, df1, df2, df3,..., df_n of arbitrary sizes and I want to pass them to various functions / methods. Passing them one at a time, foo(df1) to foo(df_n), appears to be tedius, so I want to do it in a loop.

If I create a list dfs = ['df1', 'df2',..., 'df_n'] and run a loop on the list and pass the elements, which are dataframe names, to the function, I am essentially passing strings and cannot perform the dataframe operations on strings inside the function. If I create a list dfs = [df1, df2, ..., df_n], I still cannot seem to be able access the individual dataframes using dfs[df1].

1 Answer 1

2

On constructing dfs = [df1, df2, ..., df_n], each element is a dataframe object. You need to access them as regular list using index such as dfs[0], df[1].

As your requirement, you are better with constructing dictionary

dfs = {'df1': df1, 'df2': df2, 'df3': df3}

On dictionary above, call each sub-df as dfs['df1']

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.