2

I am trying to rename a dataframe in each iteration of my for loop. For column "item" in the "data" dataframe, I would like to generate dataframes up to the number of unique items in "item" column.

for item in data.item.unique():
    data+"item" = data[data["item"] == item]

3 Answers 3

2

Use a dictionary:

frames = {}
for item in data['item'].unique():
    frames[item] = data[data['item'] == item]
Sign up to request clarification or add additional context in comments.

Comments

1

IIUC, you can just use groupby:

frames = {k:d for k,d in data.groupby('item')}

Comments

0

My advice is use dictionaries.

Your answer is this:

for i in data.item.unique():
    globals()[f'data{i}'] = data[data["item"] == i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.