Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将品牌名称列表添加到数据框中,或者如果没有显示,则添加';无';?_Python - Fatal编程技术网

Python 如何将品牌名称列表添加到数据框中,或者如果没有显示,则添加';无';?

Python 如何将品牌名称列表添加到数据框中,或者如果没有显示,则添加';无';?,python,Python,数据框“主线”有63行。问题是“品牌”专栏。每次我运行此代码时,都会出现此错误 brand_names = ["Tommy Hilfiger", "Tommy Jeans", "Hugo", "Hugo Boss", "Boss", "HUGO", "Lacoste", "lacoste", "A

数据框“主线”有63行。问题是“品牌”专栏。每次我运行此代码时,都会出现此错误


brand_names = ["Tommy Hilfiger", "Tommy Jeans", "Hugo", "Hugo Boss", "Boss", "HUGO", "Lacoste", "lacoste",
               "Adidas",
               "adidas", "Armani", "The North Face", "Paul Smith", "Vivienne Westwood", "Levis", "Kent And Curwen",
               "Nike", "BOSS", "Calvin Klein", "Kent and Curwen",
               "Pretty Green", "Lyle And Scott", "Moschino", "Converse", "Timberland", "Ralph Lauren", "Fred Perry",
               "True Religion",
               "Luke 1977", "Belstaff", "Paul And Shark", "CP Company", "Money Tri Wheel", "Money Sig", "Gant","Versace"]

image = []
title = []
price = []
link = []
shop = []
brand = []


mainline_t_shirt(soup, brand_names)

mainline = pd.DataFrame({
    'Images': image,
    'Titles': title,
    'Prices': price,
    'link': link,
    'Website': 'mainlinemenswear',
    'brand': brand

})

# Image
(code) 63 elements- code working
# Title
(code) 63 elements- code working
# Price
(code) 63 elements- code working
# link
(code) 63 elements- code working
# website
(code) 63 elements- code working

#brand
    **for container5 in title_div:
        for temp in brand_names_in:
            if temp in container5.text:
                print(temp)
                brand.append(temp)
            if temp not in container5.text:
                brand.append("None")**

这是因为并非所有品牌(耐克、阿迪达斯等)都在container.text中。如何将字符串“None”添加到行中,而不是添加品牌


需要更改的代码介于两颗星之间。

问题是,对于每个
容器5
,您都在对所有品牌进行循环。在20多个品牌中,只有一个(如果有的话)将与
container5.text
匹配。每个其他品牌都将不匹配,因此执行
brand.append(“无”)
。总共约20×
len(title\u div)
。这使得
brand
列表太大,有很多“None”(如果你
print(brand)
在循环中的某个地方或直接在循环之后,你可以看到)

您可以在此处为其他用户使用:

   raise ValueError("arrays must all be same length")
ValueError: arrays must all be same length

您的两个
if
条件实际上是一个
if…else
错误发生在该段“if temp not in container5.text:brand.append(“None”)”。如果删除,则运行正常,但我想在数据框中添加“无”星星中的代码需要更改您真的想在行中添加“无”吗?而不是列?由于“品牌”是数据框中的一列,“无”需要添加到“品牌”列中品牌名称列表中没有品牌的每一行。请注意,您的问题并不完全清楚,示例代码、示例输入数据和预期输出不完整。上面的建议是一个你可能遇到的问题的解决方案,但是可能还有其他问题潜伏着。非常好!谢谢@0
for container5 in title_div:
    for temp in brand_names_in:
        if temp in container5.text:
            brand.append(temp)
            break
    else: # not broken out of the inner for-loop, thus there was no match
        brand.append("None")