For Loop Issue with Range

What I am looking to do is loop the numbers 1 through 60 in the ‘Pick’ and then the get average of the BPM column based on that.

Let me know if you are able to help and if this is enough info the help.

Thanks!

for pick in range(1,60):

stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == pick]

stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM']))

error: tuple’ object is not callable

1 Like

Hi @adam.t.rafael: Could you provide more details on the dataframe so that I can better assist you? Aside from that try for pick in range(1,61) instead of 60 because the last specfied position is not included.

Thanks!

This is the Header, of stacked_NBA, and this was the entire code

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011]) for pick in range(1,60): stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == pick] stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM'])) print(stacked_by_pick[stacked_by_pick['Pick'] == 10])

Let me know if there is anything else I can provide, thanks!

Hi @adam.t.rafael: Could I know which line the error occurred on? Thanks

TypeError Traceback (most recent call last) <ipython-input-131-b203d4e2c2f1> in <module> 1 stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011]) ----> 2 for pick in range(1,55): 3 stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == pick] 4 stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM'])) 5 print(stacked_by_pick[stacked_by_pick['Pick'] == 10])

TypeError: ‘tuple’ object is not callable

Line 2

Hi @adam.t.rafael I think you need to use .loc here:

stacked_by_pick = stacked_NBA.loc[stacked_NBA['Pick'] == pick]

as mentioned in this article.

Also could you print(pick) before the loop and run the cell? It will help me better troubleshoot the error.

Hope this helps!

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011])
for pick in range(1,55):
print(pick)

When I ran this, it was the same error- when I ran

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011]) print(pick)

the result was ‘60’

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011])

for pick in range(1,55): stacked_by_pick = stacked_NBA.loc[stacked_NBA['Pick'] == pick] stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM'])) print(stacked_by_pick[stacked_by_pick['Pick'] == 10])

resulted in this error: TypeError Traceback (most recent call last)
in
1 stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011])
2
----> 3 for pick in range(1,55):
4 stacked_by_pick = stacked_NBA.loc[stacked_NBA[‘Pick’] == pick]
5 stacked_by_pick[‘BPM_AVG_BY_PICK’] = sum(stacked_by_pick[‘BPM’])/len((stacked_by_pick[‘BPM’]))```

TypeError: ‘tuple’ object is not callable

Not sure if this helps, but when I tried this code:

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011]) stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == 7] stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM'])) print(stacked_by_pick.head(9))

to test out a number, it worked:

Rk Pick Team Name College Years Games MP
6 7 7 CHI Kirk Hinrich Kansas 13.0 879.0 27015.0
6 7 7 PHO Luol Deng Duke 15.0 902.0 30941.0
6 7 7 TOR Charlie Villanueva UConn 11.0 656.0 13578.0
6 7 7 BOS Randy Foye Villanova 11.0 752.0 19279.0
6 7 7 MIN Corey Brewer Florida 12.0 814.0 18738.0
6 7 7 LAC Eric Gordon Indiana 12.0 663.0 21755.0
6 7 7 GSW Stephen Curry Davidson 11.0 699.0 23998.0
6 7 7 DET Greg Monroe Georgetown 9.0 632.0 17475.0
6 7 7 SAC Bismack Biyombo NaN 9.0 636.0 12864.0

   PTS     TRB  ...  MP(PG)  PTS(PG)  TRB(PG)  AST(PG)     WS  WS/48  BPM  \

6 9594.0 2576.0 … 30.7 10.9 2.9 4.8 52.5 0.093 0.0
6 13361.0 5468.0 … 34.3 14.8 6.1 2.3 74.0 0.115 0.9
6 6808.0 3019.0 … 20.7 10.4 4.6 0.8 22.6 0.080 -0.6
6 7729.0 1686.0 … 25.6 10.3 2.2 2.8 23.5 0.058 -1.2
6 7092.0 2279.0 … 23.0 8.7 2.8 1.5 22.5 0.058 -1.6
6 10991.0 1634.0 … 32.8 16.6 2.5 2.9 34.3 0.076 -0.4
6 16419.0 3158.0 … 34.3 23.5 4.5 6.6 103.2 0.207 6.4
6 8326.0 5229.0 … 27.7 13.2 8.3 2.1 49.7 0.136 1.4
6 3273.0 3968.0 … 20.2 5.1 6.2 0.5 25.3 0.095 -2.6

VORP Year BPM_AVG_BY_PICK
6 13.3 2003 0.255556
6 22.9 2004 0.255556
6 4.7 2005 0.255556
6 3.8 2006 0.255556
6 2.0 2007 0.255556
6 8.7 2008 0.255556
6 50.7 2009 0.255556
6 15.2 2010 0.255556
6 -1.9 2011 0.255556

Hello @adam.t.rafael , Please use markdown when adding code, this will increase readability without it being difficult to read code.

Find Here full guideline -

Apologies for that - thank you for the guidance on that.

The markdown cell I created to describe the cell is : "The goal of the next cell is to calculate the average BPM by pick. This is accomplished by combining the CSVs together into one dataframe, and then running a loop to calculate the average per pick. "

The code itself is:

stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == 7]
stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM']))
print(stacked_by_pick.head(9))

Thanks again

Hello @adam.t.rafael,

It should be enclosed in a pair of (```) not (''').

It is the back quote symbol (```)

Kindly edit your previous post to enhance readability as @DishinGoyani advised.

Also, @adam.t.rafael, If I got your question right, you are trying to create a BPM_AVG_BY_PICK column in the stacked_by_pick Dataframe through values 1-60 in your PICK column.

where the value in the BPM_AVG_BY_PICK column should be sum(stacked_by_pick[‘BPM’])/len((stacked_by_pick[‘BPM’])).

Is that what you are trying to achieve?

Just edited for the backquote, apologies on that (still new to this, will do better in the future haha)

And yes, the goal is to create a BPM_AVG_BY_PICK column in the stacked_by_pick dataframe where it takes the sum(stacked_by_pick[‘BPM’])/len((stacked_by_pick[‘BPM’])) per each pick.

For clarification purposes, this is what the loop I tried creating:

for pick in range(1,60):
    stacked_by_pick = stacked_NBA[stacked_NBA['Pick'] == pick]
stacked_by_pick['BPM_AVG_BY_PICK'] = sum(stacked_by_pick['BPM'])/len((stacked_by_pick['BPM']))
print(stacked_by_pick.head(9))```

@adam.t.rafael,
It is fine, you are doing great!

Kindly edit all your previous responses under this post so that one can easily follow up with the codes used from the beginning of this post.

To address this error message,

Read up on this TypeError: 'x' object is not callable.

I hope this helps.

Thank you for all of the help! Wanted to post a different workaround I did instead, and now I just need to figure out how to get the dictionary values back into the df:

stacked_NBA = pd.concat([NBA_2003,NBA_2004,NBA_2005,NBA_2006,NBA_2007,NBA_2008,NBA_2009,NBA_2010,NBA_2011])
cleaned_stack_NBA = stacked_NBA.loc[:,['Pick','BPM']]
average_pick_BPM = {}
unique_picks = cleaned_stack_NBA.loc[:,'Pick'].unique()

for p in unique_picks:
    stacked_by_pick = cleaned_stack_NBA[cleaned_stack_NBA.loc[:,'Pick'] == p]
    stacked_by_pick['AVG_BPM'] = (stacked_by_pick.loc[:,'BPM']).mean()
    remove_duplicates = stacked_by_pick.iloc[0]
    BPM = remove_duplicates['AVG_BPM']
    average_pick_BPM[p] = BPM