简体   繁体   中英

Python For Loop DataFrame

see below the following code I have at the moment and the error that is coming with it.

 companynames = []
for x in urls:
    website_nametext = requests.get(x)
    all_text = website_nametext.text
    all_soup = BeautifulSoup(all_text, 'html.parser')
    companynames.append(all_soup.h1.get_text())
overall_data = pd.DataFrame(data=[companynames], columns=['Company Name'])

I receive the following error

AssertionError: 1 columns passed, passed data had 3 columns

If I was to change the code to put three columns then it allows the dataframe to be created. See below the code.

    companynames = []
for x in urls:
    website_nametext = requests.get(x)
    all_text = website_nametext.text
    all_soup = BeautifulSoup(all_text, 'html.parser')
    companynames.append(all_soup.h1.get_text())
overall_data = pd.DataFrame(data=[companynames], columns=['Company Name', 'Company Name', 'Company Name'])

However I want the results 'x' , 'y' and 'z' to be in one column and each name on a separate row. rather than there being three separate columns as per my initial code however obviously that is creating an error.

How do I do so?

Try taking out the square brackets around companynames in the pd.DataFrame call. With the brackets there it is a list of lists, albeit only one, but it effectively turns it through 90 degrees.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM