[Python 3.5] How to Group By in Python in Jupyter Workbook
Hello I am new to python and I am attempting to group a dataframe to give me the mean by position.
Below is my code
import pandas as pd
#create df
nba = pd.read_csv('NBAStats.csv', index_col='Rk')
#create subset of df
nba = nba.iloc[:,[1,17]]
#find mean of USG field by Position field
nba = nba.iloc[:,[1]].mean().groupby(nba.iloc[:,[0]])
print(nba)
My expected output should be something like this:
PG 24.35
SG 17.24
SF 24.14
Now I have a few questions.
-
What am I doing wrong
-
Why do I need to use iloc instead of simply saying nba[1,17]?
-
I get an error when I try to select fields from my dataframe by name, and am forced to call them by column order, why might that be?
If you could help I would greatly appreciate it.
Here is the PasteBin Link: http://pastebin.com/fQmYnSMg
Here is a link to where I got the csv:
http://www.basketball-reference.com/leagues/NBA_2017_advanced.html#advanced_stats::none
Posted Dec 1, 2016, 6:16 AM UTC · edited
59