MJP
MJP

Reputation: 1597

forming a new data frame using zip, but getting an error

I have a numpy_array called playoff_teams:

playoff_teams = np.sort(playoff_seeds['team'])
playoff_teams[:7]

array([1115, 1124, 1139, 1140, 1143, 1155, 1165], dtype=int64)

I have a data_frame called reg:

       season daynum    wteam   wscore  lteam   lscore  wloc    numot
108122  2010    7       1143     75      1293    70       H     0
108123  2010    7       1314     88      1198    72       H     0
108124  2010    7       1326     100     1108    60       H     0
108125  2010    7       1393     75      1107    43       H     0
108126  2010    9       1143     95      1178    61       H     0

I then loop over the teams and perform the following action:

for teams in  playoff_teams:
    games = reg[(reg['wteam'] == teams) | (reg['lteam']== teams)]
    last_six = sum(games.tail(6)['wteam'] == teams)
    zipped = zip(team, last_six)

I get an error

TypeError: zip argument #1 must support iteration

I need to form a new data frame in following format:

col_1   col_2
team_1   last_six
team_2   last_six
team_3   last_six

How do I do that?

Upvotes: 3

Views: 148

Answers (1)

d6bels
d6bels

Reputation: 1482

sum() returns a number, not something that you can iterate over while zip() needs iterables so I think your problem is there.

last_six = sum(games.tail(6)['wteam'] == teams)  # Number
zipped = zip(team, last_six)  # Error because last_six is not iterable

You could store the results in a list (that might be a dict too) for instance :

new_data = []
for teams in  playoff_teams:
    games = reg[(reg['wteam'] == teams) | (reg['lteam']== teams)]
    last_six = sum(games.tail(6)['wteam'] == teams)
    new_data.append((teams, last_six))

Then build your data frame using DataFrame.from_items or DataFrame.from_dict (if you chose a dict and not a list).

Upvotes: 2

Related Questions