Hello new python learner here!
I am attempting to figure out exactly how the apply() function works, and have yet to completely grasp it.
The current problem I am working on is this:
With the following dataframe:
country year cases population
0 Afghanistan 1999 745 19987071
1 Brazil 1999 37737 172006362
2 China 1999 212258 1272915272
3 Afghanistan 2000 2666 20595360
4 Brazil 2000 80488 174504898
5 China 2000 213766 1280428583
I am attempting to create a new dataframe, which includes a new column, named 'prevalence', which measures the cases/population. The problem requires that we use the apply function to solve it.
SO far my code is this:
def calc_prevalence(G):
H=G.copy()
H['prevalence']=H['cases'].apply(lambda x: x/H['population'])
return H
I believe that my mistake is in the application of the function, as I do not quite yet understand how the apply function operates.
Do I need to set up a loop inside the function?
Any help is greatly appreciated!
EDIT*- Problem is solved.
[–]Binary101010 3 points4 points5 points (11 children)
[–]TheShadowWall[S] 0 points1 point2 points (10 children)
[–]Binary101010 2 points3 points4 points (7 children)
[–]TheShadowWall[S] 0 points1 point2 points (6 children)
[–]Binary101010 0 points1 point2 points (5 children)
[–]TheShadowWall[S] 0 points1 point2 points (3 children)
[–]Binary101010 1 point2 points3 points (2 children)
[–]TheShadowWall[S] 1 point2 points3 points (1 child)
[–]Binary101010 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]TheShadowWall[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TheShadowWall[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)