0

Any idea how to add the division(j) to each row?? I run the program and it runs through each division (division 1 through 5). I want to add what division it is to each row. I have the headers 'Name, Gender, State, Position, Grad, Club/HS, Rating, Commitment, Division' at the top of the table. Right now I don't know which division each row is because it is blank. Thanks for your help....

import pandas as pd

max_page_num = 10 

with open('results.csv', 'a', newline='') as f:  
    f.write('Name, Gender, State, Position, Grad, Club/HS, Rating, Commitment, Division\n')

def division():
   for j in range(1,5):
      division = str(j)  
      for i in range(max_page_num):  
         print('page:', i)
         graduation = str(2020)
         area = "commitments" # "commitments" or "clubplayer"
         gender = "m" 
         page_num = str(i)
         source = "https://www.topdrawersoccer.com/search/?query=&divisionId=" + division + "&genderId=m&graduationYear=" + graduation + "&playerRating=&pageNo=" + page_num + "&area=" + area +""
         all_tables = pd.read_html(source)
         df = all_tables[0]
         print('items:', len(df))

         df.to_csv('results.csv', header=False, index=False, mode='a') 

division()
2
  • Do you mean df['division'] = division ? Commented Feb 2, 2020 at 20:37
  • Hi misantroop: I want to add the school's division i.e. 1,2,3,4, or in a column called "division" in the table in the csv (results.csv Commented Feb 3, 2020 at 4:27

1 Answer 1

1

Simply adding the column 'division' should do it if I understand correctly.

import pandas as pd

max_page_num = 10 

with open('results.csv', 'a', newline='') as f:  
    f.write('Name, Gender, State, Position, Grad, Club/HS, Rating, Commitment, Division\n')

def division():
   for j in range(1,5):
      division = str(j)  
      for i in range(max_page_num):  
         print('page:', i)
         graduation = str(2020)
         area = "commitments" # "commitments" or "clubplayer"
         gender = "m" 
         page_num = str(i)
         source = "https://www.topdrawersoccer.com/search/?query=&divisionId=" + division + "&genderId=m&graduationYear=" + graduation + "&playerRating=&pageNo=" + page_num + "&area=" + area +""
         all_tables = pd.read_html(source)
         df = all_tables[0]
         df['division'] = division
         print('items:', len(df))

         df.to_csv('results.csv', header=False, index=False, mode='a') 

division()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.