Skip to content Skip to sidebar Skip to footer

How To Create Dataframes Iterating Over A Set?

I have this dataframe: d = {'city':['Barcelona','Madrid','Rome','Torino','London','Liverpool','Manchester','Paris'], 'country': ['ES','ES','IT','IT','UK','UK','UK','FR'], 'revenue'

Solution 1:

You can use a dictionary comprehension with groupby:

res = {k: v.drop('country', 1) for k, v in df.groupby('country')}

print(res)

{'ES':    amount       city  revenue
       08Barcelona117Madrid2,
 'FR':    amount   city  revenue
       71Paris8,
 'IT':    amount    city  revenue
       26Rome335Torino4,
 'UK':    amount        city  revenue
       44London553Liverpool662Manchester7}

Solution 2:

Country is an iterator variable that is being over written.

In order to generate 4 different dataframes, try using a generator function.

def country_df_generator(data): for country in data['country']unique(): yield df.loc[(df["country"]== country), ['date','sum']] countries = country_df_generator(data)

Solution 3:

The loop gave you all four data frames, but you threw the first three into the garbage.

You iterate through a with the variable country, but then destroy that value in the next statement, country = .... Then you return to the top of the loop, reset country to the next two-letter abbreviation, and continue this conflict through all four nations.

If you need four data frames, you need to keep each one in a separate place. For instance:

a = set(df.loc[:]["country"])
df_dict = {}

for country in a:
    df_dict[country] = df.loc[(df["country"]== country),['date','sum']]

Now you have a dictionary with four data frames, each one indexed by its country code. Does that help?

Post a Comment for "How To Create Dataframes Iterating Over A Set?"