在完成作业的过程中遇到了一些困难,在参考别的同学代码中发现他比我多了一条as_index=False,就把index的标题位置上移,为实现后面的工作提供了基础。上面说的比较抽象,在下面有实例说明。
   
  
  
   
    首先看一下pandas官方给出的groupby函数,可以看到默认值为as_index=True
   
  
  grouby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)
  下面部分是从https://stackoverflow.com/questions/41236370/what-is-as-index-in-groupby-in-pandas搬运
  
import pandas as pd
df = pd.DataFrame(data={'books':['bk1','bk1','bk1','bk2','bk2','bk3'], 'price': [12,12,12,15,15,17]})
print df
print
print df.groupby('books', as_index=True).sum()
print
print df.groupby('books', as_index=False).sum()
  Output:
  
   
    
     注意两次print输出中‘book’和‘price’的位置
    
   
  
    books  price
0   bk1     12
1   bk1     12
2   bk1     12
3   bk2     15
4   bk2     15
5   bk3     17
       price
books       
bk1       36
bk2       30
bk3       17
  books  price
0   bk1     36
1   bk2     30
2   bk3     17
  
   When
   
    as_index=True
   
   the key(s) you use in groupby will become an index in the new dataframe.
  
  
   The benefit of
   
    as_index=True
   
   is that you can yank out the rows you want by using key names. For eg. if you want
   
    'bk1'
   
   you can get it like this:
   
    df.loc['bk1']
   
   as opposed to when
   
    as_index=False
   
   then you will have to get it like this:
   
    df.loc[df.books=='bk1']
   
  
  
   Including the other main benefit of using
   
    as_index=True
   
   raised by @ayhan in comments:
   
    df.loc['bk1']
   
   would be faster because it doesn't have to traverse the entire
   
    books
   
   column to find
   
    'bk1'
   
   when it's indexed. It will just calculate the hash value of
   
    'bk1'
   
   and find it in 1 go.
  
  
   官网是这样解释的:https://
   
    pandas
   
   .pydata.org/
   
    pandas
   
   -docs/stable/reference/api/
   
    pandas
   
   .DataFrame.
   
    group
   
   by.html
as_
   
    index
   
   :bool, default
   
    True
   
   For aggregated output, return object with
   
    group
   
   labels as the
   
    index
   
   . Only ...
  
  
   #as_
   
    index
   
   =
   
    False
   
   结果的列名与之前一致
aa=chipo.
   
    group
   
   by(['item_name'],as_
   
    index
   
   =
   
    False
   
   )['quantity'].sum() 
#大类的销售金额  reset_
   
    index
   
   (drop=
   
    True
   
   ) 删除原
   
    index
   
   daleijine=df.
   
    group
   
   by(['大类名称'],as_
   
    index
   
   =
   
    False
   
   )['销售金额'].sum().sort_values(['销售金额'],ascending=
   
    False
   
   ).reset_
   
    index
   
   (drop=
   
    True
   
   )
1、场景描述
随着业务发展,数据量的越来越大,业务系统越来越复杂,拆分的概念逻辑就应运而生。数据层面的拆分,主要解决部分表数据过大,导致处理时间过长,长期占用链接,甚至出现大量磁盘IO问题,严重影响性能;业务层面拆分,主要解决复杂的业务逻辑,业务间耦合度过高,容易引起雪崩效应,业务库拆分,微服务化分布式,也是当前架构的主流方向。
2、基本概念
   
    函数
    
     pandas
    
    .DataFrame.
    
     group
    
    by
    
     参数
    
    as_
    
     index
    
    的意义
含义:as_
    
     index
    
    决定了分组使用的属性是否成为新的表格的索引,默认是as_
    
     index
    
    =
    
     True
    
    ,我的代码
    
     中
    
    常用:as_
    
     index
    
    =
    
     False
    
    .
使用作为索引只是会影响查询速度,而一般没有这样的需求。
as_
    
     index
    
    =
    
     True
    
    是常用的表格形式,而as_
    
     index
    
    =
    
     False
    
    除了表格有变化,显示也会不同。
文档 ...
   
   df = pd.DataFrame(data = {'book':['bk1','bk1','bk2','bk2','bk3'],
'price':['12','12','5','5','45']})
print(df)
print(df.
   
    group
   
   by('book',as_
   
    index
   
   =
   
    True
   
   ).sum())
print(df.
   
    group
   
   by('book',as_
   
    index
   
   =
   
    False
   
   ).sum())
output:
  books
  
 
  
   我们经常需要对某些标签或索引的局部进行累计分析,这时候需要用到
   
    group
   
   by函数了。
其
   
    中
   
   
    group
   
   by函数的as_
   
    index
   
   
    参数
   
   有以下介绍:
as_
   
    index
   
   : boolean, default
   
    True
   
   For aggregated output, return object with
   
    group
   
   labels as the
   
    index
   
   . Only relevant for DataFram...
  
  
   `agg`函数是
   
    pandas
   
   
    中
   
   的一个聚合函数,用于对数据进行聚合操作。它可以接受一个或多个聚合函数作为
   
    参数
   
   ,对分组后的数据进行聚合操作,并返回聚合后的结果。
`agg`函数的语法如下:
```python
DataFrame.
   
    group
   
   by(by=None, axis=0, level=None, as_
   
    index
   
   =
   
    True
   
   , sort=
   
    True
   
   ,
   
    group
   
   _keys=
   
    True
   
   , squeeze=
   
    False
   
   , observed=
   
    False
   
   ).agg(func, *args, **kwargs)
其
   
    中
   
   ,`by`
   
    参数
   
   用于指定按照哪些列进行分组;`func`
   
    参数
   
   用于指定聚合函数,可以是预定义的函数,也可以是自定义的函数;`*args`和`**kwargs`
   
    参数
   
   用于传递聚合函数的
   
    参数
   
   。
下面是一个简单的例子,使用`agg`函数对数据进行聚合操作:
```python
import
   
    pandas
   
   as pd
# 创建数据集
data = {
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Edward', 'Frank'],
    'gender': ['F', 'M', 'M', 'M', 'M', 'M'],
    'age': [25, 32, 18, 47, 23, 38],
    'score': [85, 72, 90, 68, 92, 78]
df = pd.DataFrame(data)
# 对数据进行分组和聚合
   
    group
   
   ed = df.
   
    group
   
   by('gender')
result =
   
    group
   
   ed.agg({'age': ['mean', 'std'], 'score': 'max'})
print(result)
输出结果如下:
              age            score
             mean        std   max
gender                            
F       25.000000        NaN    85
M       32.666667  11.198214    92
上面的代码
   
    中
   
   ,我们首先创建了一个包含姓名、性别、年龄和分数的数据集。然后,我们使用`
   
    group
   
   by`函数对数据按照性别进行分组。最后,我们使用`agg`函数对分组后的数据进行聚合操作,计算每个性别的年龄的均值和标准差,以及分数的最大值。
在`agg`函数的
   
    参数
   
   
    中
   
   ,我们使用字典来指定每个列需要进行的聚合操作。其
   
    中
   
   ,字典的键表示需要聚合的列名,字典的值可以是一个或多个聚合函数。在本例
   
    中
   
   ,我们对年龄列指定了均值和标准差两个聚合函数,对分数列指定了最大值聚合函数。