如何使用DataFrame解决Python中的语法错误?[已关闭]

4sup72z8  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(124)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
4小时前关门了。
Improve this question
我正在学习网页抓取,当我试图创建一个带有数据框的表格时遇到了一个问题。
下面是我的代码:

import requests
from bs4 import BeautifulSoup
import re
import pandas as pd

url = 'https://webscraper.io/test-sites/e-commerce/allinone/phones/touch'
page = requests.get(url) 

product_name = soup.find_all('a', class_ = 'title')
price = soup.find_all('h4', class_ = 'pull-right price')
reviews = soup.find_all('p', class_ = 'pull-right')
description = soup.find_all('p', class_ = 'description')

product_name_list = []
for i in product_name:
    names = i.text
    print(product_name_list.append(names))
    
price_list = []
for i in price:
    prices = i.text
    print(price_list.append(prices))
    
review_list = []
for i in reviews:
    review = i.text
    print(review_list.append(review))
    
description_list = []
for i in description:
    descriptions = i.text
    print(description_list.append(descriptions))

# create a table with labels and call the empty list to the table variable
table = pd.DataFrame{('Product Name':product_name_list, 'Description':description_list,
                     'Price':price_list,'Reviews':review_list)}
print(table)

输出:

line 83
    table = pd.DataFrame{('Product Name':product_name_list, 'Description':description_list,
                        ^
SyntaxError: invalid syntax

知道如何解决这个问题吗?
以创建具有"产品名称"、"评论"、"价格"和"描述"的数据列表的表。

gkl3eglg

gkl3eglg1#

看起来你正试图将字典作为参数传递给函数:

table = pd.DataFrame{('Product Name':product_name_list, 'Description':description_list,
                     'Price':price_list,'Reviews':review_list)}

如果你把它分成两行,问题就更明显了:

# create the dictionary
arg = {'Product Name':product_name_list, 'Description':description_list,
                     'Price':price_list,'Reviews':review_list}

# pass the dictionary as the argument
table = pd.DataFrame(arg)

内联:

table = pd.DataFrame({'Product Name':product_name_list, 'Description':description_list,
                     'Price':price_list,'Reviews':review_list})

感谢@thierry-lathuille提供详细信息:pd.DataFrame的签名

相关问题