python - What is the difference between pandas agg and apply function? -


I can not understand the difference between pandes .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 .

As you can see, the print statement .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   

return using :

  By_space.plie (F) # and Lt. Class 'panda.core.frame.frame'> # 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 # 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 Virgina # 102 7.1 3.0 5.9 2.1 Virginie # Oat [33]: #species #setosa 1 #versicolor 1 # By using Virgina 1 #dtype: int64   

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.

Read the docs, they are very useful There is also a group of tutorials that run around the web.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -