2

I have a loop cycling through the length of a data frame and going through a list of teams. My loop should go through 41 rows but it only does 2 rows and then stops, I have no idea why it is stalling out. It seems to me I should be cycling through the entire 41 team list but it stops after indexing two teams.

import pandas as pd

excel_data_df = pd.read_excel('New_Schedule.xlsx', sheet_name='Sheet1', engine='openpyxl')

print(excel_data_df)
print('Data Frame Above')
yahoot = len(excel_data_df)
print('Length Of Dataframe Below')
print(yahoot)

for games in excel_data_df:
    yahoot -= 1
    print(yahoot)
    searching = excel_data_df.iloc[yahoot, 0]
    print(searching)
    excel_data_df2 = pd.read_excel('allstats.xlsx', sheet_name='Sheet1', engine='openpyxl')
    print(excel_data_df2)
    finding = excel_data_df2[excel_data_df2['TEAM:'] == searching].index
    print(finding)

Here is the run log

HOME TEAM:            AWAY TEAM:
0         Portland St.             Weber St.
1               Nevada             Air Force
2                 Utah                 Idaho
3         San Jose St.           Santa Clara
4        Southern Utah  SAGU American Indian
5        West Virginia              Iowa St.
6             Missouri          Prairie View
7    Southeast Mo. St.             UT Martin
8          Little Rock       Champion Chris.
9        Tennessee St.               Belmont
10         Wichita St.           Emporia St.
11           Tennessee        Tennessee Tech
12                FGCU          Webber Int'l
13    Jacksonville St.      Ga. Southwestern
14       Northern Ill.           Chicago St.
15  Col. of Charleston         Western Caro.
16        Georgia Tech           Florida A&M
17               Rider                  Iona
18               Tulsa      Northwestern St.
19        Rhode Island              Davidson
20      Washington St.           Montana St.
21             Montana         Dickinson St.
22       Robert Morris         Bowling Green
23        South Dakota                 Drake
24            Richmond        Loyola Chicago
25    Coastal Carolina           Alice Lloyd
26        Presbyterian    South Carolina St.
27        Morehead St.                  SIUE
28       San Diego St.                   BYU
29               Siena              Canisius
30            Monmouth         Saint Peter's
31              Howard               Hampton
32           App State        Columbia Int'l
33       Southern Ill.          North Dakota
34         Norfolk St.                  UNCW
35             Niagara             Fairfield
36            N.C. A&T            Greensboro
37       Western Mich.         Central Mich.
38              DePaul                Xavier
39         Georgia St.                Carver
40      Northern Ariz.         Eastern Wash.
41        Gardner-Webb                   VMI
Data Frame Above
Length Of Dataframe Below
42
41
Gardner-Webb
                TEAM:  TOTAL POINTS:  ...  TURNOVER RATIO:  ASSIST TO TURNOVER RANK
0     Mount St. Marys            307  ...               65                    239.0
1       Saint Josephs            163  ...               28                     81.0
2    Saint Marys (CA)            518  ...               78                    114.0
3        Saint Peters            399  ...               86                    145.0
4     St. John's (NY)            656  ...              115                     73.0
..                ...            ...  ...              ...                      ...
314           Wofford            327  ...               54                    113.0
315        Wright St.            220  ...               47                    206.0
316           Wyoming            517  ...               64                     27.0
317            Xavier            582  ...               84                     12.0
318    Youngstown St.            231  ...               30                     79.0

[319 rows x 18 columns]
Int64Index([85], dtype='int64')
40
Northern Ariz.
                TEAM:  TOTAL POINTS:  ...  TURNOVER RATIO:  ASSIST TO TURNOVER RANK
0     Mount St. Marys            307  ...               65                    239.0
1       Saint Josephs            163  ...               28                     81.0
2    Saint Marys (CA)            518  ...               78                    114.0
3        Saint Peters            399  ...               86                    145.0
4     St. John's (NY)            656  ...              115                     73.0
..                ...            ...  ...              ...                      ...
314           Wofford            327  ...               54                    113.0
315        Wright St.            220  ...               47                    206.0
316           Wyoming            517  ...               64                     27.0
317            Xavier            582  ...               84                     12.0
318    Youngstown St.            231  ...               30                     79.0

[319 rows x 18 columns]
Int64Index([180], dtype='int64')
Josh
  • 353
  • 3
  • 13
  • 1
    `for games in excel_data_df.iterrows()` ??? [here](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html) – Paulo Marques Dec 18 '20 at 04:17
  • Add it as solution, worked for me! Thank you! I thought I had this running in another program where I stole it from but realized once you posted that instance only indexes a single user input and not a dataframe. Thank you!! Kudos...Any ideas why it did two? – Josh Dec 18 '20 at 04:21
  • 1
    Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Dec 18 '20 at 04:21
  • Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Dec 18 '20 at 04:21
  • @CrazyChucky, it does, thank you. Probably should have searched some more, I was going crazy as I mentioned I thought I had this working elsewhere. Anyone who can answer the question of why it ran twice? :) – Josh Dec 18 '20 at 04:25

1 Answers1

1

Use:for i in index,data in excel_data_df.iterrrows() instead.

pandas.DataFrame.iterrows

DataFrame.iterrows() Iterate over DataFrame rows as (index, Series) pairs.

shekhar chander
  • 600
  • 8
  • 14
  • Ew, gross. Please do not encourage iterrows, it is an antipattern and promotes bad practices, [see here](https://stackoverflow.com/a/55557758/4909087) – cs95 Dec 18 '20 at 04:57
  • He is new to this. List comprehension will confuse him for a while. That's why suggested this. – shekhar chander Dec 18 '20 at 06:14