向空的熊猫数据框添加一行

时间:2020-06-23 19:03:11

标签: python python-3.x pandas dataframe append

我正在尝试解析从网络上抓取的房屋数据。对于每座房子,我将特征存储在列表中。我想将每个房屋的特征(例如卧室,浴室,平方英尺等)放到同一熊猫数据框中。但是,当我尝试下面的代码时,将显示数据框的标题,但没有任何内容。我在这里想念什么?

def processData(url):
    
    #Rest of function omitted as it is not relevant to the question at hand.
    
    entry = [location, price, beds, baths, sqft, lotSize, neighborhoodMed, dom, built, hs, 
          garage, neighborhood, hType, basementSize]

    df = pd.DataFrame(columns = ["Address", "Price", "Beds", "Baths", "SqFt", "LotSize", 
                                 "NeighborhoodMedian", "DOM", "Built", "HighSchool", "Garage", 
                                "Neighborhood", "Type", "BasementSize"])

    df.append(entry) #This line doesn't work
    
    return df

输出: enter image description here

1 个答案:

答案 0 :(得分:0)

只是猜测您的要求,但请尝试以下操作

import pandas as pd

location, price, beds, baths, sqft, lotSize, neighborhoodMed, dom, built, hs, garage, neighborhood, hType, basementSize = range(
    14)
entry = [
    location, price, beds, baths, sqft, lotSize, neighborhoodMed, dom, built,
    hs, garage, neighborhood, hType, basementSize
]

columns = [
    "Address", "Price", "Beds", "Baths", "SqFt", "LotSize",
    "NeighborhoodMedian", "DOM", "Built", "HighSchool", "Garage",
    "Neighborhood", "Type", "BasementSize"
]

df = pd.DataFrame(columns=columns)

df = df.append(
    dict(zip(columns, entry)), ignore_index=True)  #This line doesn't work

print(df)