pandas 如何创建熊猫数据框字典,并将数据框返回到 Excel 工作表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22003573/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a dictionary of pandas dataframes, and return the dataframes into excel worksheets?
提问by yoshiserry
how to create a dictionary of pandas dataframes, and return the dataframes into excel worksheets?
如何创建Pandas数据框字典,并将数据框返回到 Excel 工作表中?
Hi All,
大家好,
I am learning pandas and python, and I want to create a dictionary which contains a some dataframes, which I can then run metrics over each dataframe. With each unique cluster name (one of the columns) I would like to create a dataframe (subset of original dataframe.
我正在学习 Pandas 和 python,我想创建一个包含一些数据帧的字典,然后我可以在每个数据帧上运行指标。对于每个唯一的集群名称(其中一列),我想创建一个数据框(原始数据框的子集。
Then I would like to be able to select it, run metrics over it, putting the results in a new dataframe, and then place the original dataframe (each subset) into a separate worksheet using xlsxwriter python library.
然后我希望能够选择它,对其运行指标,将结果放入新的数据帧中,然后使用 xlsxwriter python 库将原始数据帧(每个子集)放入单独的工作表中。
#create dictionary object
    c_dict = {}
#get a list of the unique names
c_dict= data.groupby('Cluster').groups
#create a dictionary of dataframes, one for each cluster
for cluster in c_dict.items():
    df = data[data['Cluster']==cluster
    c_dict[cluster] =df                                                        <<< im getting invalid syntax here
#go through the dictionary and create a worksheet and put the dataframe in it.
for k,v in c_dict.items():
    dataframe = GetDF(k)                                                            <<< creating worksheets and puts the data from the dataframe > worksheet is not working because of invalid syntax when trying to create dataframe dictionary ^^
    dataframe.to_excel(writer,sheet_name=k)
writer.save
#get the dataframe from the dictionary,
GetDF(dictionary_key)
          return c_dict[dictionary_key]
回答by munk
I think this is what you're looking for. As I said in the comments, it's probably not the right solution and it's definitely not idomatic for pandas DataFrames.
我想这就是你要找的。正如我在评论中所说,这可能不是正确的解决方案,而且对于 Pandas DataFrames 绝对不是惯用的。
import pandas as pd
groups = data.groupby('Cluster')
#create a dictionary of dataframes, one for each cluster
c_dict = {k: pd.DataFrame(v) for k, v in groups.groups.iteritems() }
If you want to save this to an excel file, the documentation is here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html
如果您想将其保存到 excel 文件,文档在此处:http: //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html
There is a nice example at the bottom that will do what you need.  Hint: use for k,v in myDict.iteritems()to get keys and values.
底部有一个很好的示例,可以满足您的需求。提示:用于for k,v in myDict.iteritems()获取键和值。

