1

How do I create a new data frame named after each entry in an array?

I have an array created from a Pandas data frame column. It looks something like below.

[In]  company_id = df.COMPANY_IDENTIFICATION.unique()
[In]  company_id
[OUT] array(['BBG000QFYJ26', 'BBG000C0ZQY2', 'BBG000LNZ408', ..., 'BBG000QXGV57',
   'BBG0022MJRB2', 'BBG0025394S5'], dtype=object)

I created a simple loop, but I don't think it's working correctly.

for i in company_id:
    i = pd.DataFrame()

As a test, I had it print i after each iteration.

for i in company_id:
    i = pd.DataFrame()
    print(i)

Output was:

Index: []

Empty DataFrame

Columns: []

Index: []

Empty DataFrame

Columns: []

etc.

I also can't call the datframes by the names I know are in the array. Thoughts?

1 Answer 1

1

A better idea is to store your dataframes in a dictionary, with names as keys:

d= {i: pd.DataFrame() for i in company_id}

Then you can refer to individual dataframes via dictionary keys, e.g. d['BBG000QFYJ26'].

You can iterate the dictionary via dict.items():

for k, v in d.items():
    # perform operation
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.