Pandas for Everyone: Python Data Analysis by Nazmul Rajib
Author:Nazmul Rajib
Language: eng
Format: epub
Publisher: Addison-Wesley
Published: 2018-04-14T04:00:00+00:00
9.3.2.1 Column-wise Operations
Use the axis=0 parameter (the default value) in apply when working with functions in a column-wise manner.
Click here to view code image
df.apply(print_me, axis=0)
0 10
1 20
2 30
Name: a, dtype: int64
0 20
1 30
2 40
Name: b, dtype: int64
a None
b None
dtype: object
Compare this output to the following:
Click here to view code image
print(df['a'])
0 10
1 20
2 30
Name: a, dtype: int64
print(df['b'])
0 20
1 30
2 40
Name: b, dtype: int64
You can see that the outputs are exactly the same. When you apply a function across a DataFrame (in this case, column-wise with axis=0), the entire axis (e.g., column) is passed into the first argument of the function. To illustrate this further, let’s write a function that calculates the mean (average) of three numbers (each column in our data set contains values).
def avg_3(x, y, z):
return (x + y + z) / 3
If we try to apply this function across our columns, we get an error.
Click here to view code image
# will cause an error
print(df.apply(avg_3))
Traceback (most recent call last):
File "<ipython-input-1-5ebf32ddae32>", line 2, in <module>
print(df.apply(avg_3))
TypeError: ("avg_3() missing 2 required positional arguments: 'y' and
'z'", 'occurred at index a')
From the (last line of the) error message, you can see that the function takes three arguments, but we failed to pass in the y and z (i.e., the second and third) arguments. Again, when we use apply, the entire column is passed into the first argument. For this function to work with the apply method, we will have to rewrite parts of it.
Click here to view code image
def avg_3_apply(col):
x = col[0]
y = col[1]
z = col[2]
return (x + y + z) / 3
print(df.apply(avg_3_apply))
a 20.0
b 30.0
dtype: float64
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26526)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19009)
The Fifty Shades Trilogy & Grey by E L James(18962)
Shot Through the Heart by Mercy Celeste(18881)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16987)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16878)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16806)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16703)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16330)
The Subtle Art of Not Giving a F*ck by Mark Manson(14262)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13425)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10924)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9478)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9317)
This is Going to Hurt by Adam Kay(9105)
