简体   繁体   English

如何根据条件从一个原始 df 创建多个 df,然后为它们分配单独的名称

[英]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).我想从一个原始df(每个国家的频率不同)中创建几个df(每个国家大约70个),然后为它们分配单独的名称(因此我使用了字典)。 But I can't access the individual df anymore.但我不能再访问个人 df 了。 They should have different names and should remain a data frame.它们应该有不同的名称,并且应该保持一个数据框。 error: 'method' object is not subscriptable错误:“方法”object 不可下标

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() .您需要使用()调用副本,即df.loc[df['CountryName'] == country].copy()

However there's no need to subset your DataFrame in a loop.但是,无需在循环中对 DataFrame 进行子集化。 This is exactly what groupby is made for and you can create the dict succinctly with这正是groupby的用途,您可以简洁地创建字典

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.这是因为 groupby object 的__iter__方法:“返回:生成器为每个组产生(名称,子集对象)序列”,因此使用单个分组键,这些值成为字典的键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据条件从 DF 创建 DF - how to create a DF from a DF based on a condition 如何在一定条件下将值从一个df复制到原始df? - How to copy values from one df to the original df with a certain condition? 如何通过将df ID#替换为另一个df的名称来创建新的df? - How do I create a new df by replace the df ID# with names of another df? python:根据列名条件创建多索引pandas DF - python: Create a multiindex pandas DF based on condition of column names Pandas:如何根据行值从一个 df 获取列标签并将其分配为新 df 中的行值? - Pandas: How do you get column labels from one df based on row values and assign these as row values in new df? 根据条件为 DF 列分配新值 - Assign new values to DF column based on a condition 如何将一个df的列条目匹配到另一个df; 如果它们相同,则将另一列的条目从第一个df附加到第二个df? - How do I match a column entry from one df to a different df; and if they're the same, append another column's entry from the first df to the 2nd df? 如何使用变量名pandas df.assign()? - How to pandas df.assign() with variable names? 我如何 plot 来自一个 DF 的多个图表,而无需每次手动重新键入图表 function - How do I plot several graphs from one DF without manually retyping the graph function each time 如何在df.assign中输入条件? - How to input a condition into df.assign?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM