1

I am trying to assign a name of the dataframe dynamically and I am failing.

The code below assigns it to the variable DataFrame_Name and not the value of the DataFrame_Name

for i in range(0, len(Required_Reports_df)):
    ReportName = Required_Reports_df.iloc[i]['Report Name']
    ReportPath = Required_Reports_df.iloc[i]['Report Path']
    DataFrame_Name = ReportName + "_df"
    DataFrame_Name  = pd.read_excel (ReportPath, skiprows=[ReportStartingHeaderRow])

What are my options here, other than having a dictionary. If Dictionary is the best option, how can I use dictionary here - Should I consider creating the dictionary with another for loop before this.

Kindly help me with this.

1 Answer 1

1

I am trying to assign a name of the dataframe dynamically....If Dictionary is the best option

A dictionary is indeed recommended practice:

dfs = {}  # define empty dictionary to store your dataframes

# construct iterable of name, path pairs
zipper = zip(Required_Reports_df['Report Name'], Required_Reports_df['Report Path'])

# iterate name, path pairs and add items to dictionary
for name, path in zipper:
    dfs[name] = pd.read_excel(path)

Once you are familiar with this pattern, you can rewrite more efficiently as a dictionary comprehension:

dfs = {name: pd.read_excel(path) for name, path in zipper}
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.