學習如何掌握Python中的groupby函數

學習如何掌握Python中的groupby函數


ONe在Pandas軟件包中流行的功能是群比功能。我相信,幾乎所有以前使用Pandas的人也必須使用群比功能。它是如此流行,因為它提供了總結,但詳細的結果有效。正如潘達斯軟件包醫生所描述的,

這是小編準備的python基礎學習資料,關注,轉發,私信小編“01”即可免費領取!

學習如何掌握Python中的groupby函數


我們指的是一個涉及以下一個或多個步驟的過程:

根據一定的準則將數據劃分成組。

對每一組獨立地應用一個函數。

將結果組合成數據結構。[1]

SQL中還有一個groupby函數。因此,對於在SQL方面有經驗的人來說,用Python學習groupby函數並不是一件難事。但問題是Pandas中的groupby可以執行比在SQL中更多的分析,這使得Pandas中的groupby成為一種常見但必不可少的功能。

“靈丹妙藥”中的群比之所以更強大,是因為第二步的“應用”。在SQL中,“應用”步驟中的大多數操作在統計上是相關的,如min、max、count等。然而,在Pandas中,“應用”可以執行得更多。

從潘達斯的醫生那裡,

在應用步驟中,我們可能希望使用以下內容之一:

聚合:計算每個組的彙總統計數據(或統計數據)。

轉換:執行一些特定於組的計算,並返回一個類似的索引對象。

過濾:根據評估真假的分組計算,丟棄一些組.[1]

在本文中,我將介紹一些Groupby應用程序。這些應用程序不僅向我展示了從數據中獲得的洞察力,而且還幫助我識別了我在分析數據方面的下一步行動。

我們開始吧。

本文使用的數據是Kaggle的“學生酒精消費”中的“學生-por.csv”。您可以從鏈接 .

<code># Input

import

pandas

as

pd

data

= pd.read_csv(

'student-por.csv'

)

data

.info()# Output<

class

'

pandas

.

core

.

frame

.

DataFrame

'>

RangeIndex:

649

entries,

0

to

648

Data columns (total

33

columns): school

649

non-

null

object

sex

649

non-

null

object

age

649

non-

null

int64 address

649

non-

null

object

famsize

649

non-

null

object

Pstatus

649

non-

null

object

Medu

649

non-

null

int64 Fedu

649

non-

null

int64 Mjob

649

non-

null

object

Fjob

649

non-

null

object

reason

649

non-

null

object

guardian

649

non-

null

object

traveltime

649

non-

null

int64 studytime

649

non-

null

int64 failures

649

non-

null

int64 schoolsup

649

non-

null

object

famsup

649

non-

null

object

paid

649

non-

null

object

activities

649

non-

null

object

nursery

649

non-

null

object

higher

649

non-

null

object

internet

649

non-

null

object

romantic

649

non-

null

object

famrel

649

non-

null

int64 freetime

649

non-

null

int64 goout

649

non-

null

int64 Dalc

649

non-

null

int64 Walc

649

non-

null

int64 health

649

non-

null

int64 absences

649

non-

null

int64 G1

649

non-

null

int64 G2

649

non-

null

int64 G3

649

non-

null

int64 dtypes: int64(

16

),

object

(

17

) memory usage:

167.4

+ KB/<code>


分享到:


相關文章: