Was ist der Unterschied zwischen Pandas Agg und Apply-Funktionen auf Groupby?Python

Python-Programme
Anonymous
 Was ist der Unterschied zwischen Pandas Agg und Apply-Funktionen auf Groupby?

Post by Anonymous »

Ich kann den Unterschied zwischen den Funktionen .aggregate und .apply von Pandas nicht herausfinden.

Nehmen Sie das Folgende als Beispiel: Ich lade einen Datensatz, führe ein Groupby durch, definiere eine einfache Funktion
und verwende entweder .agg oder .apply.
Wie Sie vielleicht sehen, führt die Druckanweisung in meiner Funktion zur gleichen Ausgabe
nach der Verwendung von .agg und .apply. Das Ergebnis ist hingegen ein anderes. Warum ist das so?

Code: Select all

import pandas
import pandas as pd
iris = pd.read_csv('iris.csv')
by_species = iris.groupby('Species')
def f(x):
...:     print type(x)
...:     print x.head(3)
...:     return 1
Verwenden von apply:

Code: Select all

by_species.apply(f)
#
#   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
#0           5.1          3.5           1.4          0.2  setosa
#1           4.9          3.0           1.4          0.2  setosa
#2           4.7          3.2           1.3          0.2  setosa
#
#   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
#0           5.1          3.5           1.4          0.2  setosa
#1           4.9          3.0           1.4          0.2  setosa
#2           4.7          3.2           1.3          0.2  setosa
#
#    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width     Species
#50           7.0          3.2           4.7          1.4  versicolor
#51           6.4          3.2           4.5          1.5  versicolor
#52           6.9          3.1           4.9          1.5  versicolor
#
#     Sepal.Length  Sepal.Width  Petal.Length  Petal.Width    Species
#100           6.3          3.3           6.0          2.5  virginica
#101           5.8          2.7           5.1          1.9  virginica
#102           7.1          3.0           5.9          2.1  virginica
#Out[33]:
#Species
#setosa        1
#versicolor    1
#virginica     1
#dtype: int64
Agg verwenden

Code: Select all

by_species.agg(f)
#
#   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
#0           5.1          3.5           1.4          0.2  setosa
#1           4.9          3.0           1.4          0.2  setosa
#2           4.7          3.2           1.3          0.2  setosa
#
#    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width     Species
#50           7.0          3.2           4.7          1.4  versicolor
#51           6.4          3.2           4.5          1.5  versicolor
#52           6.9          3.1           4.9          1.5  versicolor
#
#     Sepal.Length  Sepal.Width  Petal.Length  Petal.Width    Species
#100           6.3          3.3           6.0          2.5  virginica
#101           5.8          2.7           5.1          1.9  virginica
#102           7.1          3.0           5.9          2.1  virginica
#Out[34]:
#           Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
#Species
#setosa                 1            1             1            1
#versicolor             1            1             1            1
#virginica              1            1             1            1

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post