Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

Here is a simple example of the code I am running, and I would like the results put into a pandas dataframe (unless there is a better option):

for p in game.players.passing():

    print p, p.team, p.passing_att, p.passer_rating()

R.Wilson SEA 29 55.7

J.Ryan SEA 1 158.3

A.Rodgers GB 34 55.8

Using this code:

d = []

for p in game.players.passing():

    d = [{'Player': p, 'Team': p.team, 'Passer Rating':

        p.passer_rating()}]

pd.DataFrame(d)

I can get:

    Passer Rating   Player      Team

     0    55.8     A.Rodgers   GB

Which is a 1x3 dataframe, and I understand why it is only one row but I can't figure out how to make it multi-row with the columns in the correct order. Ideally the solution would be able to deal with n number of rows (based on p) and it would be wonderful (although not essential) if the number of columns would be set by the number of stats requested. Any suggestions? Thanks in advance!

1 Answer

0 votes
by (41.4k points)

 Using list comprehension:

from pandas import DataFrame as df

d = df[[p, p.team, p.passing_att, p.passer_rating()] for p in game.players.passing()]

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...