Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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 分配分组结果_Python_Pandas - Fatal编程技术网

Python 分配分组结果

Python 分配分组结果,python,pandas,Python,Pandas,我有以下数据帧: date, industry, symbol, roc 25-02-2015, Health, abc, 200 25-02-2015, Health, xyz, 150 25-02-2015, Mining, tyr, 45 25-02-2015, Mining, ujk, 70 26-02-2015, Health, abc, 60 26-02-2015, Health, xyz, 310 26-02-

我有以下数据帧:

date,       industry, symbol, roc
25-02-2015, Health,   abc,    200
25-02-2015, Health,   xyz,    150
25-02-2015, Mining,   tyr,    45
25-02-2015, Mining,   ujk,    70
26-02-2015, Health,   abc,    60
26-02-2015, Health,   xyz,    310
26-02-2015, Mining,   tyr,    65
26-02-2015, Mining,   ujk,    23
我需要确定平均‘roc’、最大‘roc’、最小‘roc’以及每个日期+行业的符号数量。换句话说,我需要按日期和行业分组,然后确定各种平均值、最大值/最小值等

到目前为止,我正在做以下工作,虽然很有效,但似乎非常缓慢且效率低下:

sector_df = primary_df.groupby(['date', 'industry'], sort=True).mean()
tmp_max_df = primary_df.groupby(['date', 'industry'], sort=True).max()
tmp_min_df = primary_df.groupby(['date', 'industry'], sort=True).min()
tmp_count_df = primary_df.groupby(['date', 'industry'], sort=True).count()
sector_df['max_roc'] = tmp_max_df['roc']
sector_df['min_roc'] = tmp_min_df['roc']
sector_df['count'] = tmp_count_df['roc']
sector_df.reset_index(inplace=True)
sector_df.set_index(['date', 'industry'], inplace=True)
上面的代码起作用,生成一个按日期+行业索引的数据框,显示每个日期+行业的最小/最大“roc”是多少,以及每个日期+行业存在多少符号

我基本上做了多次完整的分组(以确定“roc”的平均值、最大值、最小值和计数)。这是非常缓慢的,因为它一遍又一遍地做着同样的事情


有没有办法只做一次小组练习。然后对该对象执行平均值、最大值等,并将结果分配给要使用
agg
执行聚合的扇区

In [72]:

df.groupby(['date','industry']).agg([pd.Series.mean, pd.Series.max, pd.Series.min, pd.Series.count])
Out[72]:
                       roc                
                      mean  max  min count
date       industry                       
2015-02-25 Health    175.0  200  150     2
           Mining     57.5   70   45     2
2015-02-26 Health    185.0  310   60     2
           Mining     44.0   65   23     2
这允许您传递要执行的函数的iterable(在本例中是一个列表)

编辑

要访问单个结果,需要为每个轴传递一个元组:

In [78]:
gp.loc[('2015-02-25','Health'),('roc','mean')]

Out[78]:
175.0

其中,
gp=df.groupby(['date','industry']).agg([pd.Series.mean,pd.Series.max,pd.Series.min,pd.Series.count])
您想使用
agg
执行聚合:

In [72]:

df.groupby(['date','industry']).agg([pd.Series.mean, pd.Series.max, pd.Series.min, pd.Series.count])
Out[72]:
                       roc                
                      mean  max  min count
date       industry                       
2015-02-25 Health    175.0  200  150     2
           Mining     57.5   70   45     2
2015-02-26 Health    185.0  310   60     2
           Mining     44.0   65   23     2
这允许您传递要执行的函数的iterable(在本例中是一个列表)

编辑

要访问单个结果,需要为每个轴传递一个元组:

In [78]:
gp.loc[('2015-02-25','Health'),('roc','mean')]

Out[78]:
175.0

其中,
gp=df.groupby(['date','industry']).agg([pd.Series.mean,pd.Series.max,pd.Series.min,pd.Series.count])

您只需将groupby部分保存到一个变量中,如下所示:

primary_df = pd.DataFrame([['25-02-2015', 'Health', 'abc', 200],
                   ['25-02-2015', 'Health', 'xyz', 150],
                   ['25-02-2015', 'Mining',  'tyr', 45],
                   ['25-02-2015', 'Mining', 'ujk', 70], 
                   ['26-02-2015', 'Health', 'abc', 60],
                   ['26-02-2015', 'Health', 'xyz', 310],
                   ['26-02-2015', 'Mining',  'tyr', 65],
                   ['26-02-2015', 'Mining', 'ujk', 23]], 
                  columns='date industry symbol roc'.split())

grouped = primary_df.groupby(['date', 'industry'], sort=True)
sector_df = grouped.mean()
tmp_max_df = grouped.max()
tmp_min_df = grouped.min()
tmp_count_df = grouped.count()

sector_df['max_roc'] = tmp_max_df['roc']
sector_df['min_roc'] = tmp_min_df['roc']
sector_df['count'] = tmp_count_df['roc']
sector_df.reset_index(inplace=True)
sector_df.set_index(['date', 'industry'], inplace=True)

您只需将groupby部件保存到变量,如下所示:

primary_df = pd.DataFrame([['25-02-2015', 'Health', 'abc', 200],
                   ['25-02-2015', 'Health', 'xyz', 150],
                   ['25-02-2015', 'Mining',  'tyr', 45],
                   ['25-02-2015', 'Mining', 'ujk', 70], 
                   ['26-02-2015', 'Health', 'abc', 60],
                   ['26-02-2015', 'Health', 'xyz', 310],
                   ['26-02-2015', 'Mining',  'tyr', 65],
                   ['26-02-2015', 'Mining', 'ujk', 23]], 
                  columns='date industry symbol roc'.split())

grouped = primary_df.groupby(['date', 'industry'], sort=True)
sector_df = grouped.mean()
tmp_max_df = grouped.max()
tmp_min_df = grouped.min()
tmp_count_df = grouped.count()

sector_df['max_roc'] = tmp_max_df['roc']
sector_df['min_roc'] = tmp_min_df['roc']
sector_df['count'] = tmp_count_df['roc']
sector_df.reset_index(inplace=True)
sector_df.set_index(['date', 'industry'], inplace=True)

太好了,谢谢。在创建df后,我将如何引用特定值,例如,我将如何获得2015年2月25日《健康》中roc平均值。我尝试了以下不起作用的方法:df.loc['2015-02-25','Health']['roc','mean']@darkpool我已经更新了我的答案以显示如何访问单个值谢谢,我收到了一个关键错误。忙着弄明白为什么会这样。我猜是这里出了问题,而不是你的答案。我会尽快接受你的回答,只要我能弄清楚为什么我会得到这个键错误。我会检查你的索引和列,以确保没有输入错误,还有你的日期日期时间数据类型是否正确?它现在工作了,谢谢。我忘了重置,然后设置了索引。谢谢,太好了,谢谢。在创建df后,我将如何引用特定值,例如,我将如何获得2015年2月25日《健康》中roc平均值。我尝试了以下不起作用的方法:df.loc['2015-02-25','Health']['roc','mean']@darkpool我已经更新了我的答案以显示如何访问单个值谢谢,我收到了一个关键错误。忙着弄明白为什么会这样。我猜是这里出了问题,而不是你的答案。我会尽快接受你的回答,只要我能弄清楚为什么我会得到这个键错误。我会检查你的索引和列,以确保没有输入错误,还有你的日期日期时间数据类型是否正确?它现在工作了,谢谢。我忘了重置,然后设置了索引。非常感谢。