Home > Enterprise >  Iterate rows of a data frame and create a new data frame
Iterate rows of a data frame and create a new data frame

Time:10-26

Goal: I want to create one data frame per NFL game week looking like this:

Week 1
Home                       Away
Arizona Cardinals          Tennessee Titans
Los Angeles Chargers       Washington Football Team
.
.
.

Week 2
Home                       Away
.
.
.

What do I have: a data frame of the schedule as grid:

print(df_sched)
0   Week      Arizona Cardinals           Atlanta Falcons      Baltimore Ravens  ...    San Francisco 49ers       Tampa Bay Buccaneers       Tennessee Titans Washington Football Team
1      1      @Tennessee Titans       Philadelphia Eagles    @Las Vegas Raiders  ...         @Detroit Lions             Dallas Cowboys      Arizona Cardinals     Los Angeles Chargers
2      2      Minnesota Vikings     @Tampa Bay Buccaneers    Kansas City Chiefs  ...   @Philadelphia Eagles            Atlanta Falcons      @Seattle Seahawks          New York Giants
3      3  @Jacksonville Jaguars          @New York Giants        @Detroit Lions  ...      Green Bay Packers          @Los Angeles Rams     Indianapolis Colts           @Buffalo Bills
4      4      @Los Angeles Rams  Washington Football Team       @Denver Broncos  ...       Seattle Seahawks      @New England Patriots         @New York Jets         @Atlanta Falcons

Proposed solution:

  1. I would create an empty df_temp=['Away', 'Home'] and start filling it using iterrows.
  2. Based on if there is an '@' in the row value, the team in the row is the 'Home'-Team, else the team in the column header is the 'Home'-Team.
  3. The result should be one df per game week (df_week1, df_week2, ..., df_week18)

CodePudding user response:

import pandas as pd


cols = ['Week','Arizona Cardinals','Atlanta Falcons','Baltimore Ravens',
        'San Francisco 49ers','Tampa Bay Buccaneers','Tennessee Titans',
        'Washington Football Team']

data = [[1,'@Tennessee Titans','Philadelphia Eagles','@Las Vegas Raiders','@Detroit Lions','Dallas Cowboys','Arizona Cardinals','Los Angeles Chargers'],
      [2,'Minnesota Vikings','@Tampa Bay Buccaneers','Kansas City Chiefs','@Philadelphia Eagles','Atlanta Falcons','@Seattle Seahawks','New York Giants'],
      [3,'@Jacksonville Jaguars ','@New York Giants','@Detroit Lions','Green Bay Packers','@Los Angeles Rams','Indianapolis Colts','@Buffalo Bills'],
      [4, '@Los Angeles Rams','Washington Football Team','@Denver Broncos','Seattle Seahawks','@New England Patriots','@New York Jets','@Atlanta Falcons']]

df = pd.DataFrame(data, columns=cols)


dfs = {}
for idx, row in df.iterrows():
    rows = []
    for alpha, beta in row.iteritems():
        if alpha == 'Week':
            df_name = 'df_week%.2d'            
  • Related