Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dataframe groupby强制转换为dataframe_Python_Pandas_Export To Excel - Fatal编程技术网

Python 将dataframe groupby强制转换为dataframe

Python 将dataframe groupby强制转换为dataframe,python,pandas,export-to-excel,Python,Pandas,Export To Excel,我只需要将DataFrameGroupBy对象强制转换为DataFrame,以便使用df.to_excel()导出到excel。当我尝试执行df_groupby=pd.DataFrame(df_groupby)时,我得到一个错误:PandasError:DataFrame构造函数没有正确调用 原始df: df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', '

我只需要将DataFrameGroupBy对象强制转换为DataFrame,以便使用
df.to_excel()
导出到excel。当我尝试执行
df_groupby=pd.DataFrame(df_groupby)
时,我得到一个错误:
PandasError:DataFrame构造函数没有正确调用

原始df:

 df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                       'foo', 'bar', 'foo', 'foo'],
                 'B' : ['one', 'one', 'two', 'three',
                        'two', 'two', 'one', 'three'],
                 'C' : randn(8), 'D' : randn(8)})

In [2]: df
Out[2]: 
 A      B         C         D
 0  foo    one  0.469112 -0.861849
 1  bar    one -0.282863 -2.104569
 2  foo    two -1.509059 -0.494929
 3  bar  three -1.135632  1.071804
 4  foo    two  1.212112  0.721555
 5  bar    two -0.173215 -0.706771
 6  foo    one  0.119209 -1.039575
 7  foo  three -1.044236  0.271860
grouped=df.groupby('A')


我想将
分组
导出到excel。

我不确定您想要实现什么。所以这里有一组可能的答案

但首先是坏消息。groupby对象不是数据框,不能保存到excel(或简单地转换为数据框)

1) 如果您只想对数据帧进行排序,这也将“分组”数据帧

2) 如果您想摆脱Excel中的默认索引

df.sort('A').to_excel('filename.xls', index=None)
3) 如果希望每个组在Excel中各自的工作表中

grouped = df.groupby('A')
from pandas import ExcelWriter
writer = ExcelWriter('filename.xls')
for k, g in grouped:
    g.to_excel(writer, k)
writer.save()
4) 您可以将这些组连接到一个新的数据帧中。但这与上面的第一个排序选项几乎相同

grouped = df.groupby('A')
new_df = pd.DataFrame()
for k, g in grouped:
    new_df = pd.concat([new_df, g], axis=0)
new_df.to_excel('filename.xls')
5) 无意义的第一次练习。。。传递变换函数。。。但这只是给你的数据帧回来

df = df.set_index('A')
grouped = df.groupby(level=0)
grouped.transform(lambda x: x).to_excel('filename.xls')
6) 另一个无意义的练习。。。这一次使用了直通过滤器功能

# start with initial data
grouped = df.groupby('A')
grouped.filter(lambda x: True).to_excel('filename.xls')
7) 如果您想查看groupby的内部,您可以始终执行以下操作。。。(但请注意,这不会保存到Excel)


groupby对象不存在;'在你做手术之前不要做任何事。你能在这里展示一下你想要实现的目标吗,因为听起来你可能只需要设置一个多索引。我有一个我分组的原始df,我想将结果导出到excel,除了它现在是一个dataframe groupby对象。请展示原始数据和代码来演示你的问题。要点是,当您对groupby对象执行操作时,它将返回df或series。如果您只是对列进行分组,那么这只是如何执行分组的元数据。更新后显示了一个示例。正如我已经说过的,对列进行分组会生成一个描述如何执行分组的对象,您是否希望这样:
df.set_index('a')。to_excel()
# start with initial data
grouped = df.groupby('A')
grouped.filter(lambda x: True).to_excel('filename.xls')
# start with initial data
grouped = df.groupby('A')
groups = dict(list(grouped))
print[groups['foo'])
print[groups['bar'])