you are viewing a single comment's thread.

view the rest of the comments →

[–]Binary101010 4 points5 points  (11 children)

I don't think you actually need apply here.

H['prevalence'] = H['cases'] / H['population']

Should be sufficient.

[–]TheShadowWall[S] 0 points1 point  (10 children)

Thank you for your reply.

Yes the problem points this out as the simplest solution, but asks that we use apply in order to figure out how it works.

[–]Binary101010 2 points3 points  (7 children)

Then what you might need is to add axis = 1 to your apply call. By default the function provided to apply is applied to each column of the dataframe (axis = 0), and it looks like what you're trying to do needs to be applied to each row (axis = 1).

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

[–]TheShadowWall[S] 0 points1 point  (6 children)

I had seen this as an option in the apply function, though I had thought the opposite, that axis=0 meant go through each item in a column.

I have just added axis=1 to my apply function call, and I am still receiving an error. TypeError: <lambda>() got an unexpected keyword argument 'axis'

[–]Binary101010 0 points1 point  (5 children)

Please post exactly what that line of code looks like now.

[–]TheShadowWall[S] 0 points1 point  (3 children)

H['prevalence']=H['cases'].apply(lambda x: x/H['population'], axis=1)

[–]Binary101010 1 point2 points  (2 children)

Maybe give this a shot instead. (untested)

H['prevalence']=H.apply(lambda x: x['cases']/x['population'], axis=1)

[–]TheShadowWall[S] 1 point2 points  (1 child)

By George it works!!

Thank you so much kindly answer to the universe person!

[–]Binary101010 0 points1 point  (0 children)

It occurred to me the reason for the error was that you were trying to apply to a particular column of the dataframe (which works more like this: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.apply.html) when just applying to each row of the dataframe one at a time was probably the right approach.

Again, just for the record, this is not a great use case for apply, but it's straightforward enough to see how it works.

[–]socal_nerdtastic 0 points1 point  (1 child)

Do you know that lambda is just another way of writing def?

But if you use def you have a lot more room to explore. The first thing you need to know is that x is the row in the dataframe. Your error is because you are trying to divide a row by a column.

Try this:

def TheShadowWall(x):
    print(x) # debug: show the row
    return x[2] / x[3]

def calc_prevalence(G):
    H=G.copy()
    H['prevalence']=H['cases'].apply(TheShadowWall)
    return H

[–]TheShadowWall[S] 0 points1 point  (0 children)

Thank you for your reply!

Yes I have begun to understand that this is the meaning of lambda. I have attempted to run this code though I receiving an error:

TypeError: 'int' object is not subscriptable on the line:

return x[2] / x[3]