Joseph Mann
Joseph Mann

Reputation: 25

For loops with numpy/pandas (iterating over column headers)

I have a pandas dataframe (players) with headers (passes, tackles....)

I am looking to get a correlation coefficient for a few of the columns (not all)

for stats in [passes,tackles,shots,saves]:
    cc = np.corrcoef(players.minutes, players.stats)[1,0]
    print cc

However, my code returns

name 'passes' is not defined

because passes is not a defined variable. Is there an easy way to iterate over certain columns for this type of analysis?

Upvotes: 1

Views: 160

Answers (1)

user2510479
user2510479

Reputation: 1540

for stats in ['passes','tackles','shots','saves']:
    cc = np.corrcoef(players['minutes'], players[stats])[1,0]
    print cc

Upvotes: 3

Related Questions