2

I am webscraping housing data and putting it into a pandas dataframe. I wanted to test my function for one listing before continuing on several. My only problem is the way the entry is being added to the dataframe.

enter image description here

The listing information is being added as a column instead of a row (I want it added as a row). Here is what I am doing. Is there a better function to use? Thanks in advance!

entry = pd.DataFrame([location, price, beds, baths, sqft, lotSize, neighborhoodMed, dom, built, 
          garage, neighborhood, hType, basementSize])

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

Side note: location, price, beds, etc. are all values. (e.g., a location could be 123 Main St, a price could be 495,000, etc.)

324
  • 702
  • 8
  • 28

2 Answers2

1

One option would be to convert 'entry' to a dictionary rather than a df and then use df.append:

entry_dict = {'Address': location,
              'Price': price,
              [...]
             }

df = df.append(entry_dict, ignore_index = True)
Chris Schmitz
  • 618
  • 1
  • 6
  • 16
0

One way to this is to call df.iloc[new_row's_index] = [address_value, price_value,...] You can follow the same pattern with .loc as long as you index by what the name of the row is, and not the index position (though it appears to be the same in your case)

gellerm
  • 47
  • 1
  • 7