任务:我正在尝试从字典列表创建一个pandas数据框。问题:这会为每个字典项创建一个数据帧

2024-05-19 11:04:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从我使用webscraped数据生成的三个列表中创建一个数据框。但是,当我尝试将这些列表转换为字典,然后使用它们来构建我的pandas dataframe时,它会为每个字典项(行)输出一个dataframe,而不是一个dataframe,该dataframe将所有这些项作为dataframe中的行包含在内

我相信问题在于我用来收集数据的for循环。我知道在这个问题上也有人问过类似的问题,包括这里Pandas DataFrame created for each row和这里Take multiple lists into dataframe,但我尝试过解决方案,但毫无乐趣。我相信webscrape循环增加了一个细微差别,这使得这个问题变得更加棘手

下面是我的代码和输出的逐步演练,作为参考,我已将熊猫作为pd和bs4导入

    # Step 1 create a webscraper which takes three sets of data (price, bedrooms and bathrooms) from a website and populate into three separate lists

for container in containers:
    try:
        price_container=container.find("a",{"class":"listing-price text-price"})
        price_strip=price_container.text.strip()
        price_list=[]
        price_list.append(price_strip)

    except TypeError:
        continue

    try:
        bedroom_container = container.find("span",{"class":"icon num-beds"})
        bedroom_strip=(bedroom_container["title"])
        bedroom_list=[]
        bedroom_list.append(bedroom_strip)
    
     except TypeError:
        continue

    try:
        bathroom_container=container.find("span", {"class":"icon num-baths"})
        bathroom_strip=(bathroom_container["title"])
        bathroom_list=[]
        bathroom_list.append(bathroom_strip)
    
    except TypeError:
        continue

# Step 2 create a dictionary 

    data = {'price':price_list, 'bedrooms':bedroom_list, 'bathrooms':bathrooms_list}


# Step 3 turn it into a pandas dataframe and print the output

    d=pd.DataFrame(data)
    print(d)    

这为每个字典提供了一个数据框架,如下所示

   price               bedrooms          bathrooms                                   
0  £200,000            3                 2

[1 rows x 3 columns]
  

   price               bedrooms          bathrooms                                   
0  £400,000            5                 3

[1 rows x 3 columns]


   prices              bedrooms          bathrooms                                   
0  £900,000            6                 4

[1 rows x 3 columns]

and so on.....

我尝试了字典理解和列表理解,为每个字典项提供一个数据帧而不是一个数据帧:

data = [({'price':price, 'bedrooms':bedrooms, 'bathrooms':bathrooms}) for item in container]

df = pd.DataFrame(data)

print(df)

而且,不管我如何处理列表表达式,这会产生更奇怪的输出。它为字典中的每一项提供了一个数据帧,同一行信息重复了很多次

   price               bedrooms          bathrooms                                  
0  £200,000            3                 2
0  £200,000            3                 2
0  £200,000            3                 2

[3 rows x 3 columns]
  

   price               bedrooms          bathrooms                                   
0  £400,000            5                 3
0  £400,000            5                 3
0  £400,000            5                 3

[3 rows x 3 columns]


   price               bedrooms          bathrooms                                   
0  £900,000            6                 4
0  £900,000            6                 4
0  £900,000            6                 4
[1 rows x 3 columns]

and so on...

如何解决此问题并将所有数据放在一个数据帧中


Tags: columnsand数据dataframedata字典containerprice
3条回答

您在这里测试的代码部分很好-列表字典将始终返回单个数据帧。所以这一部分:

pd.DataFrame(data)

不可能是问题的原因。事实上,它被隐藏在一个循环中,所以运行了三次。这同样适用于您的列表,这些列表被反复定义

把这些部分从循环中去掉,你应该会没事的

您必须合并这三个列表

df = pd.DataFrame(data["price"] + data["bedrooms"] + data["bathrooms"] )

如果您想要更通用的东西:

list_ = [item for i in data for item in data[i]]
df = pd.DataFrame(list_)

首先,您应该在for循环之前执行price_list=[]bedroom_list=[]bathroom_list=[],否则它们最多只有1个元素,因为每次循环都会重置为[],然后附加单个元素。第二,如果您希望有单个数据帧,您应该在for循环之外创建它,即dedentdata = {'price':price_list, 'bedrooms':bedroom_list, 'bathrooms':bathrooms_list} 和下面的句子。最后,在缺少数据的情况下,您应该表示它-如果除了第一个continue之外的任何数据都将被执行,那么您的price_listbedroom_listbathroom_list将具有不同的长度。我建议用price_list.append(None)替换第一个continue,用bedroom_list.append(None)替换第二个bedroom_list.append(None),用bathroom_list.append(None)替换第三个bathroom_list.append(None),这样在数据帧中就可以清楚地指示数据丢失的位置

相关问题 更多 >

    热门问题