python - What is the difference between pandas agg and apply function? -
I can not understand the difference between pandes As you can see, the print statement return Read the docs, they are very useful There is also a group of tutorials that run around the web. .groupate and
.apply functions.
As an example below: I load a dataset, do a by group , define a simple function, and either user
.gg or
.apply .
.gg and
.apply within my work. The result, on the other hand, is different why is it?
PD iris = pd.read_csv ('iris.csv') by_species = iris.groupby ('species') import as DF F (x) Panda import pandes: ..: Print Type (x) ...: print x.head (3) ...: 1
using :
AGG
by_species.agg (f) # & lt; Class 'pandas.core .frame.DataFrame' & gt; # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 5.1 5.1 3.5 1.4 Setosa # 1 4.9 3.0 1.4 0.2 Setosa # 2 4.7 3.2 1.3 0.2 Setosa # & lt; Class 'pandas.core.frame.DataFrame' & gt; # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 50 7.0 3.2 4.7 1.4 VernalLOROR # 51 6.4 3.2 4.5 1.5 Vernacular # 52 6.9 3.1 4.9 1.5 VernalLore # & lt; Class 'pandas.core.frame.DataFrame' & gt; # 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 Virginia # 102 7.1 3.0 5.9 2.1 Virginia # Oat [34]: # Sepell Lamp Sepal.Width Petal Length Petal.Width #Species #setosa 1 1 1 # Varsiloer 1 1 1 1 # Wurbibenate 1 1 1 1
Apply The function applies to each group (your
species ). Your function returns 1, so you end up with 1 value for every 3 groups.
agg set each column for each group (feature) so you end up with a column per value per column.
Comments
Post a Comment