程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python pandas groupby]

編輯:Python

pandas groupby 數據聚合與分組

  • GroupBy Four grouping keys
    • DataFrame列名的值
    • A dictionary or a dict that can match values ​​up the grouping axis to the grouping nameSeries
      • Series
      • 字典
    • A list or array of values ​​of the same length as the axis to be grouped
    • A function that can be called on the axis index or on a single label within the index
  • **groupby().get_group()**

groupby語法

df.groupby(
by=None,
axis=0,
level=None,
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
observed: bool = False,
)

GroupBy Four grouping keys

DataFrame列名的值

  • where the column name can be a single column name,Can also be an array containing multiple column names
  • After the column names become groupedindex行索引的名字
  • as_index設成False的時候,Disable grouping key as row index

A dictionary or a dict that can match values ​​up the grouping axis to the grouping nameSeries

Series


字典

The keys in the dictionary do not need to contain all of themindex,也可以包含indexkey not in

A list or array of values ​​of the same length as the axis to be grouped

  • When using a list of values,分組的indexNo more default names

A function that can be called on the axis index or on a single label within the index

作為分組鍵傳遞的函數將會按照每個索引值調用一次,同時返回值會被用作分組名稱.

groupby().get_group()

pandas按照列groupby後,You can take the corresponding value according to the valuegroup.
When done on multiple columnsgroupby時,get_groupWhen you need to enter multiple columns of valuestuple,比如get_group((‘a’, 1)),其中’a’為第一個groupa value in the column,1為第二個groupa value in the column.

import pandas as pd
df = pd.DataFrame({
'col1':['a', 'b', 'c','a', 'b', 'c'], 'col2':[1,2,3,4,5,6]})
df
>>> col1 col2
0 a 1
1 b 2
2 c 3
3 a 4
4 b 5
5 c 6
df.groupby('col1').get_group('a')
>>> col1 col2
0 a 1
3 a 4
df.groupby(['col1', 'col2']).get_group(('a', 1))
>>> col1 col2
0 a 1

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved