Giving different names to df using a for loop

Question:

I have a df in python with different cities.

enter image description here

I am trying to create a df for each city.

So wrote this code in python and it works. It does what I need. But i was wondering if there is any over way to create the name of each df in a different way rather than using

globals()["df_"+str(ciudad)] = new_grouped_by

If I try this:

"df_"+str(ciudad) = new_grouped_by

Give me this error: SyntaxError: can't assign to operator

Any tips/suggestions would be more than welcome!

def get_city():
    for ciudad in df["Ciudad"].unique():
        #print (ciudad)
        grouped_by = df.groupby('Ciudad')
        new_grouped_by=[grouped_by.get_group(ciudad) for i in grouped_by.groups]
        globals()["df_"+str(ciudad)] = new_grouped_by

get_city()
Asked By: Christian

||

Answers:

A simple way would be to store the dataframes in a dictionary with the city names as keys:

import pandas as pd

data = zip(['Amsterdam', 'Amsterdam', 'Barcelona'],[1,22,333])
df = pd.DataFrame(data, columns=['Ciudad', 'data'])
new_dfs = dict(list(df.groupby('Ciudad')))

Calling new_dfs['Amsterdam'] will then give you the dataframe:

Ciudad data
0 Amsterdam 1
1 Amsterdam 22
Answered By: RJ Adriaansen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.