Python熊貓– GroupBy

Python熊貓– GroupBy (Python Pandas – GroupBy)

GroupBy method can be used to work on group rows of data together and call aggregate functions. It allows to group together rows based off of a column and perform an aggregate function on them.

GroupBy方法可用于一起處理分組數據行并調用聚合函數。 它允許基于列將行分組在一起,并對它們執行聚合功能。

Consider the below example, there are three partitions of IDS (1, 2, and 3) and several values for them. We can now group by the ID column and aggregate them using some sort of aggregate function. Here we are sum-ing the values and putting the values.

考慮下面的示例,有三個IDS分區(1、2和3),以及它們的幾個值。 現在,我們可以按ID列進行分組,并使用某種聚合函數對其進行聚合。 在這里,我們將這些值相加并放入這些值。

Python Pandas GroupBy

與熊貓團購 (Groupby with Pandas)

Create a dataframe from a dictionary

從字典創建數據框

import numpy as np
import pandas as pd
data = {'company':['Google','Microsoft','FB','Google','FB'], 'person':['Molly','Nathaniel', 'Sriansh', 'Carl','Sarah'], 'Sales':[200,123,130,144,122]}
df = pd.DataFrame(data)
print(df)

Output

輸出量

     company     person  Sales
0     Google      Molly    200
1  Microsoft  Nathaniel    123
2         FB    Sriansh    130
3     Google       Carl    144
4         FB      Sarah    122

Following examples illustrate the 'GroupBy' function,

以下示例說明了“ GroupBy”功能

Example 1: GroupBy by 'company'

示例1:按“公司”分組

# returns the groubBy object
print(df.groupby('company')) 
'''
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f1721585350>
'''
by_company = df.groupby('company')
#invoke aggregate function
print(by_company.mean()) 
'''
Sales
company
FB           126
Google       172
Microsoft    123
'''

In the above example, we don't see the person column, because the data type is String and by no means, we can get mean of String variables, and hence Pandas automatically ignores any non-numeric values.

在上面的示例中,我們沒有看到person列,因為數據類型是String ,但絕不能獲得String變量的均值 ,因此Pandas自動忽略任何非數字值。

Below are some more examples of aggregate functions,

以下是聚合函數的更多示例,

print(by_company.sum())
'''
Output:
Sales
company
FB           252
Google       344
Microsoft    123
'''

print(by_company.std())
'''
Output:
Sales
company
FB          5.656854
Google     39.597980
Microsoft        NaN
'''

Note the return type of the values are by default a DataFrame, as illustrated below,

請注意,默認情況下,值的返回類型為DataFrame,如下所示,

std = by_company.std()
print(type(std))
'''
Output:
<class 'pandas.core.frame.DataFrame'>
'''

And, hence we can perform all the dataFrame functions such as,

并且,因此我們可以執行所有dataFrame函數,例如,

print(by_company.std().loc['FB'])
'''
Output:
Sales    5.656854
Name: FB, dtype: float64
'''

The above mentioned steps, all can be performed in a single step as follows,

上述步驟全部可以在一個步驟中執行,如下所示:

print(df.groupby('company').sum().loc['FB'])
'''
Output:
Sales    252
Name: FB, dtype: int64
'''

Some more aggregate functions are,

還有一些聚合函數,

print(df.groupby('company').count())
'''
Output:
person  Sales
company
FB              2      2
Google          2      2
Microsoft       1      1
'''
print(df.groupby('company').max())
'''
Output:
person  Sales
company
FB           Sriansh    130
Google         Molly    200
Microsoft  Nathaniel    123
'''
print(df.groupby('company').min())
'''
Output:
person  Sales
company
FB             Sarah    122
Google          Carl    144
Microsoft  Nathaniel    123
'''

使用具有描述方法的GroupBy (Using GroupBy with describe method)

The describe() method returns a bunch of useful information all at once.

describe()方法一次返回一堆有用的信息。

print(df.groupby('company').describe())
'''
Output:
Sales                    ...
count   mean        std  ...    50%    75%    max
company                            ...
FB          2.0  126.0   5.656854  ...  126.0  128.0  130.0
Google      2.0  172.0  39.597980  ...  172.0  186.0  200.0
Microsoft   1.0  123.0        NaN  ...  123.0  123.0  123.0
[3 rows x 8 columns]
'''

The format of the description can be changed using transpose() method,

可以使用transpose()方法更改描述的格式,

print(df.groupby('company').describe().transpose())
'''
Output:
company              FB     Google  Microsoft
Sales count    2.000000    2.00000        1.0
mean   126.000000  172.00000      123.0
std      5.656854   39.59798        NaN
min    122.000000  144.00000      123.0
25%    124.000000  158.00000      123.0
50%    126.000000  172.00000      123.0
75%    128.000000  186.00000      123.0
max    130.000000  200.00000      123.0
'''

翻譯自: https://www.includehelp.com/python/python-pandas-groupby.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377264.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377264.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MySQL索引底層原理理解以及常見問題總結

目錄二叉查找樹為索引紅黑樹為索引B樹作為索引B樹作為索引MyISAM存儲引擎索引實現InnoDB存儲引擎索引實現常見問題聚集索引與非聚集索引InnoDB基于主鍵索引和普通索引的查詢有什么區別&#xff1f;InnoDB主鍵索引為何是整型的自增主鍵何時使用業務字段作為主鍵呢&#xff1f;哈…

Spring之HibernateTemplate 和HibernateDaoSupport

spring提供訪問數據庫的有三種方式&#xff1a; HibernateDaoSupport HibernateTemplate&#xff08;推薦使用&#xff09; jdbcTemplate(我們一般不用&#xff09; 類所在包&#xff1a; HibernateTemplate&#xff1a;org.springframework.orm.hibernate3.HibernateTemplate …

JDOJ-重建二叉樹

這是一道面試題&#xff0c;可以說是數據結構中的基礎題了&#xff0c;由先序遍歷以及中序遍歷生成一棵樹&#xff0c;然后輸出后序遍歷。 一個遞歸函數傳遞5個參數&#xff0c;頂點編號&#xff0c;先序左右區間&#xff0c;中序左右區間&#xff0c;每次進行區間長度判定&…

des算法密碼多長_密碼學中的多個DES

des算法密碼多長This is a DES that was susceptible to attacks due to tremendous advances in computer hardware in cryptography. Hence, it was a very complex or competent algorithm it would be feasible to reuse DES rather than writing an of cryptography. 由于…

《MySQL——索引筆記》

目錄回表覆蓋索引最左前綴原則聯合索引的時候&#xff0c;如何安排索引內的字段順序&#xff1f;索引下推重建索引問題聯合主鍵索引和 InnoDB 索引組織表問題in與between的區別回表 回到主鍵索引樹搜索的過程&#xff0c;我們稱為回表。 覆蓋索引 覆蓋索引就是在這次的查詢中…

計算凸多邊形面積的算法

1. 思路&#xff1a; 可以將凸多邊形&#xff08;邊數n > 3&#xff09;劃分為 (n - 2) 個三角形&#xff0c;分別運用向量叉積計算每個三角形的面積&#xff0c;最后累加各個三角形的面積就是多邊形的面積。 2. 求多邊形面積的算法模板&#xff1a;   定義點的結構體 str…

Windows CE開發常見問題解答

轉自&#xff1a; http://blog.csdn.net/slyzhang/article/details/6110490 1.怎樣在一個控件獲得焦點時打開軟鍵盤&#xff1f;比如一個EditBox獲得焦點后&#xff0c;這個時候自動打開軟鍵盤&#xff0c;這樣可以方便用戶輸入——SIPINFO、SHSIPINFO、SIPSETINFO、SIPGETINFO…

Julia中的supertype()函數

Julia| supertype()函數 (Julia | supertype() function) supertype() function is a library function in Julia programming language, it is used to get the concrete supertype of the given type (data type). supertype()函數是Julia編程語言中的庫函數&#xff0c;用于…

《操作系統知識點整理》

目錄進程與線程比較多線程同步與互斥生產者與消費者哲學家就餐問題讀者寫者問題進程間通信管道消息隊列共享內存信號量信號Socket鎖互斥鎖與自旋鎖讀寫鎖樂觀鎖與悲觀鎖死鎖進程與線程比較 進程是資源&#xff08;包括內存、打開的文件等&#xff09;分配的單位&#xff0c;線…

for,foreach,iterator的用法和區別

相同點&#xff1a; 三個都可以用來遍歷數組和集合不同點&#xff1a;1.形式差別 for的形式是 for&#xff08;int i0;i<arr.size();i&#xff09;{...} foreach的形式是 for&#xff08;int i&…

和菜鳥一起學linux總線驅動之初識spi驅動主要結構

既然知道了協議了&#xff0c;那么就可以開始去瞧瞧linux kenerl中的spi的驅動代碼了&#xff0c;代碼中有很多的結構體&#xff0c;還是對主要的結構體先做個了解吧&#xff0c;那樣才可以很好的理解驅動。主要是include/linux/spi.h 首先是SPI的主機和從機通信接口&#xff0…

操作系統大內核和微內核_操作系統中的內核

操作系統大內核和微內核A Kernel is the central component of an Operating System. The Kernel is also said to be the heart of the Operating System. It is responsible for managing all the processes, memory, files, etc. The Kernel functions at the lowest level …

《MySQL——鎖》

全局鎖是什么&#xff1f;全局鎖有什么用&#xff1f;全局鎖怎么用&#xff1f; 全局鎖主要用在邏輯備份過程中&#xff0c;對于InnoDB 引擎的庫&#xff0c;使用–single-transaction; MySQL 提供了一個加全局讀鎖的方法&#xff0c;命令是 Flush tables with read lock (FTW…

搜索引擎Constellio及Google Search Appliances connectors

做搜索產品的時候發現國外一個同類型的產品contellio&#xff0c;發現功能比較強大&#xff0c;先記錄下來 貌似可以添加文檔 網站 以及數據庫等不同類型的數據源 http://wiki.constellio.com/index.php/Main_Page http://www.constellio.com/ http://www.constellio.com htt…

dig下載_DIG的完整形式是什么?

dig下載DIG&#xff1a;副監察長 (DIG: Deputy Inspector General) DIG is an abbreviation of the Deputy Inspector General. It is a high-level position in the Indian Police Service. The officers who already offered service on Senior Superintendent of Police (SS…

分類器是如何做檢測的?——CascadeClassifier中的detectMultiScale函數解讀

原地址&#xff1a;http://blog.csdn.net/delltdk/article/details/9186875 在進入detectMultiScal函數之前&#xff0c;首先需要對CascadeClassifier做初始化。 1. 初始化——read函數 CascadeClassifier的初始化很簡單&#xff1a; cv::CascadeClassifier classifier; cl…

<MySQL>何時使用普通索引,何時使用唯一索引

如果能夠保證業務代碼不會寫入重復數據&#xff0c;就可以繼續往下看。 如果業務不能保證&#xff0c;那么必須創建唯一索引。 關于查詢能力 普通索引和唯一索引在查詢能力上是沒有很大差別的。 如&#xff1a;select id from T where k5 1、普通索引查找到滿足條件的第一個記…

Web版OutLook,利用POP接收郵件服務器郵件

一直想做一個Web版的OutLook&#xff0c;所以才萌生這個想法&#xff0c;其實以前也接觸過這方面的東西。于是上網找了找&#xff0c;漫天的都是Jmail來接收&#xff0c;好吧&#xff0c;既然大家都在用我也就下載下來試試了。 什么&#xff0c;怎么總是報錯呢&#xff1f;原來…

abs std::abs_ABS的完整形式是什么?

abs std::absABS&#xff1a;防抱死制動系統 (ABS: Anti-lock Braking System) ABS is an abbreviation of the Anti-lock Braking System. It is a safety anti-skid braking system that is used on a variety of aircraft, automobiles and other land vehicles, such as mo…

ubuntu 使用

shell 命令歷史搜索 &#xff1a; ctrl r使能 session 選擇界面&#xff1a;安裝gnome-session-fallback安裝lwqq轉載于:https://www.cnblogs.com/JonnyLulu/p/3600263.html