0

I'm currently encountering an error with my code, and I have no idea why. I originally thought it was because I couldn't save a csv file with hyphens in it, but that turns out not to be the case. Does anyone have any suggestions to what might be causing the problem. My code is below:

import pandas as pd
import requests

query_set = ["points-per-game"]

for query in query_set:
    url = 'https://www.teamrankings.com/ncaa-basketball/stat/' + str(query)
    html = requests.get(url).content
    df_list = pd.read_html(html)
    print(df_list)

    df_list.to_csv(str(query) + "stat.csv", encoding="utf-8")
2
  • the error is pretty clear - you are attempting to call .to_csv to a python list (which would obviously fail) Commented Feb 15, 2021 at 4:40
  • 1
    as stated below you need to call the dataframe element from the list. Also, no need for html = requests.get(url).content as pandas .read_html() has the capablities to request the html. simply can do df_list = pd.read_html(url) Commented Feb 15, 2021 at 10:17

2 Answers 2

1

the read_html() method returns a list of dataframes, not a single dataframe:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_html.html

You'll want to loop through your df_list and run to_csv on each entry, like so:

import pandas as pd import requests

query_set = ["points-per-game"]

for query in query_set:
    url = 'https://www.teamrankings.com/ncaa-basketball/stat/' + str(query)
    html = requests.get(url).content
    df_list = pd.read_html(html)
    print(df_list)

    for current_df in df_list:
      current_df.to_csv(str(query) + "stat.csv", encoding="utf-8")
      print(current_df)
Sign up to request clarification or add additional context in comments.

Comments

1

The function pd.read_html returns a list of DataFrames found in the HTML source. Use df_list[0] to get the DataFrame which is the first element of this list.

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.