[英]Split Pandas Dataframe Into Sub Dataframes (not list of dataframes)
I am trying to split a dataframe into as many different dataframes as possible, and set the name of the new dataframe based on information being split. 我正在尝试将一个数据框拆分为尽可能多的不同数据框,并根据要拆分的信息设置新数据框的名称。
EX: 例如:
call this dataframe 'df' 将此数据框称为“ df”
Name ID Number Code Name2
123 cp1 500 ABC 456
123 cp1 501 DEF 456
I am trying to split this dataframe into chunks based on 'Number', and then rename these new dataframes accordingly. 我正在尝试根据“数字”将此数据框拆分为多个块,然后相应地重命名这些新数据框。
The result would look like the following: 结果将如下所示:
df500:
Name ID Number Code Name2
123 cp1 500 ABC 456
df501:
Name ID Number Code Name2
123 cp1 501 DEF 456
Does pandas allow for something like this to be done? 熊猫允许这样做吗?
Create a dictionary by grouping: 通过分组创建字典:
d={'df_{}'.format(i):g for i,g in df.groupby('Number')}
{'df_500': Name ID Number Code Name2
0 123 cp1 500 ABC 456.0, 'df_501': Name ID Number Code Name2
1 123 cp1 501 DEF NaN}
You can then call the keys of the dict like: 然后,您可以像这样调用字典的键:
print(d['df_500'])
Name ID Number Code Name2
0 123 cp1 500 ABC 456.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.