简体   繁体   中英

How do I create several df's out of one original df based on a condition and then assign them individual names

df_collection = {}
for country in country_names:
   df_collection[country] = df.loc[df['CountryName'] == country].copy

I want to create several df's (about 70 for each country one) out of one original df (each country is differing in frequency) and then assign them individual names (therefore I used a dictionary). But I can't access the individual df anymore. They should have different names and should remain a data frame. error: 'method' object is not subscriptable

Does anyone have a solution?

You assigned a method to each of your dictionary keys. You need to call copy with () , ie df.loc[df['CountryName'] == country].copy() .

However there's no need to subset your DataFrame in a loop. This is exactly what groupby is made for and you can create the dict succinctly with

df_collection = dict(tuple(df.groupby('CountryName')))

This works because the __iter__ method of a groupby object: "Returns: Generator yielding sequence of (name, subsetted object) for each group" so with a single grouping key, those values become the keys of your dictionary.

Sample

print(df)
#  CountryName  Data
#0           a     8
#1           c     4
#2           b     4
#3           a     1
#4           a     1
#5           c     7

df_collection = dict(tuple(df.groupby('CountryName')))
## If you care for the subset defined in some list `country_names`, subset first
# df_collection = dict(tuple(df[df.CountryName.isin(country_names)].groupby('CountryName')))

df_collection['a']
#  CountryName  Data
#0           a     8
#3           a     1
#4           a     1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM