Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 使用groupby后数据帧中的键丢失_Python_R_Group By_Aggregate - Fatal编程技术网

Python 使用groupby后数据帧中的键丢失

Python 使用groupby后数据帧中的键丢失,python,r,group-by,aggregate,Python,R,Group By,Aggregate,我对Python非常陌生,只是遇到了一个问题 mini_agg是我的原创pandas.dataframe,我正在尝试分组 它由两列组成 trial = mini_agg.groupby(['date','product','product_type_1','product_type_2','product_type_3','product_type_4']).sum() print mini_agg.shape print trial.shape 输出: (2965909,10) (4992

我对Python非常陌生,只是遇到了一个问题

mini_agg
是我的原创
pandas.dataframe
,我正在尝试分组 它由两列组成

trial = mini_agg.groupby(['date','product','product_type_1','product_type_2','product_type_3','product_type_4']).sum()

print mini_agg.shape
print trial.shape
输出:

(2965909,10)
(499281,4)

此外,我无法访问按其分组的密钥。 在R中,当使用聚合时,我会返回我的列

你能帮帮我吗?提前谢谢你

刚刚找到了我以前的问题没有找到的答案:

trial = mini_agg.groupby(['date','product','product_type_1','product_type_2','product_type_3','product_type_4']).sum().reset_index()
添加
.reset_index()

刚刚找到了我以前的问题没有找到的答案:

trial = mini_agg.groupby(['date','product','product_type_1','product_type_2','product_type_3','product_type_4']).sum().reset_index()

添加
.reset_index()

就足够了,我希望提供mini_agg值,但我认为它是两个一维标记数据结构的组合。因此,正如您所提到的,mini_agg是一个
pandas.dataframe
,您必须知道
dataframe
类似于
Series
有可能接受另一个
dataframe
作为输入:

因此,如果mini_agg与以下类似:

import pandas as pd
FRAME= {'one' : pd.Series([1., 2., 3.], index=['product_type_1', 'product_type_2', 'product_type_3']),
'two' : pd.Series([1., 2., 3., 4.], index=['product_type_1', 'product_type_2', 'product_type_3', 'product_type_4'])}
mini_agg = pd.DataFrame(FRAME)
所以


我希望提供mini_agg值,但我认为它是两个一维标记数据结构的组合。因此,正如您所提到的,mini_agg是一个
pandas.dataframe
,您必须知道
dataframe
类似于
Series
有可能接受另一个
dataframe
作为输入:

因此,如果mini_agg与以下类似:

import pandas as pd
FRAME= {'one' : pd.Series([1., 2., 3.], index=['product_type_1', 'product_type_2', 'product_type_3']),
'two' : pd.Series([1., 2., 3., 4.], index=['product_type_1', 'product_type_2', 'product_type_3', 'product_type_4'])}
mini_agg = pd.DataFrame(FRAME)
所以


请将mini_agg值包括在您提供的代码中。请将mini_agg值包括在您提供的代码中