在创建 Dataframe 时遇到问题

avkwfej4  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(279)

我使用requests和beautiful soup提取一些wikipedia数据,然后我想将其放入pandas数据框(所有单元格的列名为column1)。我在尝试调用 Dataframe 时遇到此错误:
valueerror:未正确调用 Dataframe 构造函数!
有人有什么建议吗?这是我的密码:

import pandas as pd 
import requests
import bs4
result = requests.get("https://en.wikipedia.org/wiki/United_States")

# Put the data into BS

soup = bs4.BeautifulSoup(result.text,"lxml")

# select any soup element

soup.select('p')

# take the result from the list element

Results = soup.select('p')[2].getText()

# print to csv

df = pd.DataFrame(Results, columns=['Column1'])  
print(df)
efzxgjgh

efzxgjgh1#

结果是一个文本字符串:

In [36]: Results
Out[36]: "The United States of America (U.S.A. or USA), commonly known as the United States (U.S. or US) or America, is a country primarily located in North America. It consists of 50 states, a federal district, five major unincorporated territories, 326 Indian reservations, and some minor possessions.[g] At 3.8\xa0million square miles (9.8\xa0million square kilometers), it is the world's third- or fourth-largest country by total area.[c] It borders Canada to the north and Mexico to the south. With a population of more than 328.2 million people, it is the third most populous country in the world. The national capital is Washington, D.C., and the most populous city is New York City.\n"

你可以使用 io ```
pd.DataFrame(io.StringIO(Results), columns=['Column1'])

或者正如亨利·埃克所指出的那样 `[]` 围绕结果

pd.DataFrame([Results], columns=['Column1'])

现在索引位于0,第1列有结果

Column1
0 The United States of America (U.S.A. or USA), commonly known as the United States (U.S. or US) or America, is a country primarily located in North America. It consists of 50 states, a federal district, five major unincorporated territories, 326 Indian reservations, and some minor possessions.[g] At 3.8 million square miles (9.8 million square kilometers), it is the world's third- or fourth-largest country by total area.[c] It borders Canada to the north and Mexico to the south. With a population of more than 328.2 million people, it is the third most populous country in the world. The national capital is Washington, D.C., and the most populous city is New York City.\n

相关问题