简体   繁体   English

从列表创建 Dataframe

[英]Creating Dataframe from Lists

I am seeing 5 items in each list below, but it looks like I am running into issues with the shape of my data when creating my dataframe. Any idea why this error is happening?我在下面的每个列表中看到 5 个项目,但看起来我在创建 dataframe 时遇到了数据形状问题。知道为什么会发生此错误吗?

Code:代码:

b = {'Link':links, 'Tax':taxes, 'Description':descrip}
bet = pd.DataFrame(b)
['http://www.redfin.com/IL/Chicago/195-N-Harbor-Dr-60601/unit-509/home/14093313', 'http://www.redfin.com/IL/Chicago/1235-N-Astor-St-60610/unit-3N/home/13054822', 'http://www.redfin.com/MO/St-Louis/2622-S-11th-St-63118/home/93686930', 'http://www.redfin.com/IL/Chicago/426-W-Barry-Ave-60657/unit-408/home/13373863', 'http://www.redfin.com/IL/Chicago/310-S-Michigan-Ave-60604/unit-1608/home/45513284']
['$631', '$859', '$377', '$201', '$575']
["Valet parking included! Floor to ceiling bay windows with spectacular views of Navy Pier and Lake Michigan. Open kitchen features newer stainless steel appliances, granite countertops and new dishwasher and washer/dryer. Wood floor in living room and den, nice designer sliding door for second bedroom. Both bathrooms are remodeled recently. Fantastic location, steps away from Millennium Park, Navy Pier, DuSable Harbor, beautiful lakeshore east park, bike paths, Gems World Academy, restaurants, Mariano's, museums, theatre district and more. Many amenities in building include rooftop pool, jacuzzi, fitness center, two party rooms (56th floor library/party room with postcard views of lake), tennis court, grilling and pet relief areas. Perfect for in town, investment or home!", 'Very easy and safe to show! Sellers are highly motivated and can close quickly. Very best value in the building and in the neighborhood. Charming and sophisticated 4-bedroom plus den 2.1 bath penthouse in Gold Coast on quiet and wonderful tree lined Astor Street. Much larger square footage and more rooms than many other comparatively priced co-ops. Small and beautiful vintage elevator building (only 9 units) that is very well cared for and cleaned daily by two person live- in engineering/custodial team. Staff is full service to the building. Unit is very wide and bright with four exposures. Large rooms that flow-great for entertaining in the front-- and privacy and quiet in the back-- perfect for live-in, family visits and could be wonderful multiple "work from home" offices. Hardwood floors, gas fireplace, eat in Poggenpohl kitchen, cozy den, laundry in unit, ADT security system and great storage. Charming and bright sunroom with views down Astor. 50% financing allowed. Healthy reserve fund and no major capital projects planned. Ample space to spread out in this home. One dog or one cat allowed. Assessments are very reasonable and include real estate taxes. Building has an exercise room, kids\' playroom, storage room, bike room and two separate private patios. Patios have garden setting, along with seating and grills. Exterior garden has won many awards from the Gold Coast Neighbors Association. Healthy association and well maintained building. Steps to Lake Shore Drive, Beach, the Red Line train, Oak Street shopping, Northwestern Memorial Hospital, Michigan Ave walking distance to Latin School, Walter Payton, Xavier Warde, St Chrysostom\'s, Lincoln Park High School, Ogden Elementary School district. Multiple parking options right nearby. Assessment is as follows: $2,502.40 (Base Assmt. ) + $1,837.60 (2018 RE Taxes)= $4,340 (Total Assmt. ).', 'When you decide to leave your backyard oasis that includes hot tub, pool & multiple decks, you won’t lose your parking spot ! 2 car garage & garage space for TWO golf carts is yours! Dining & drinks are mere steps away. This renovated (2010) historic Founder Home is three stories; entering into a first floor parlor/dining and kitchen w/½ bath; mudroom off the kitchen;2nd flr master suite w/ walk in closet with own private deck, walk in jacuzzi tub & separate shower; full bath on 3rd flr for the 2 extra bds - perfect for your guests for Mardi Gras and brewery tours! Let’s talk about the (finished) basement – there’s indoor and exterior access, and it’s just begging to be your own pub! Plumbed wetbar & .5 bath – separate office area or storage w/2nd walk out. Oh yeah… there’s 2nd floor laundry too! It’s checking all the right boxes! Here’s some of the boring details that are important: approx. 2200 sq ft plus, new roof: 2020, zoned hvac, working gas fireplace, gas line run to grill.', "PERFECT EAST LAKEVIEW LOCATION IN ELEVATOR BUILDING. Own your own private condo for less than the cost of renting! Fantastic location near the lakefront, park, Belmont and Diversey Harbor, LAC, Mariano's, Broadway and Clark Street shopping and dining, Trader Joes, Belmont El + bus lines, LSD, etc. Welcome home to this condo located on a beautiful tree-lined street. Completely move-in ready, not a thing to do! This home has been meticulously maintained and features custom window coverings, USB outlets and updated lighting. Additional storage included. The building offers common laundry (get it all done in one hour) and secured entry. Rental parking available or street parking. No rental cap! Click on the 3D tour and take a walk around before booking your private showing.", 'Prime location in the heart of the Loop. Corner 3 BD 2BA or 2BD + den split floor plan w/ balcony. Hardwood floors in living space and kitchen with carpet in bedrooms and window coverings throughout. Open kitchen w/ granite, stainless steel appliances, island, breakfast bar etc. Master bedroom suite has large walk in closet and master bath w/ radiant heated floor, soaking tub, double vanity sinks and separate glass shower. Large storage on same floor! Wonderful amenities including fitness center, whirlpool, sauna, outdoor terrace and 24 hr doorman. Parking additional $35,000. Grant Park, steps to Millennium Park, Art Institute and more!']

Error:错误:

Traceback (most recent call last):
  File "C:\Users\Python\Price Tracking\Real Estate\RealEstate-Scraping.py", line 79, in <module>
    bet = pd.DataFrame(b, index=[1])
  File "C:\Python38\lib\site-packages\pandas\core\frame.py", line 435, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "C:\Python38\lib\site-packages\pandas\core\internals\construction.py", line 254, in init_dict
    return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "C:\Python38\lib\site-packages\pandas\core\internals\construction.py", line 74, in arrays_to_mgr
    return create_block_manager_from_arrays(arrays, arr_names, axes)
  File "C:\Python38\lib\site-packages\pandas\core\internals\managers.py", line 1675, in create_block_manager_from_arrays
    construction_error(len(arrays), arrays[0].shape, axes, e)
  File "C:\Python38\lib\site-packages\pandas\core\internals\managers.py", line 1694, in construction_error
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (5, 3), indices imply (1, 3)

I am seeing 5 items in each list below, but it looks like I am running into issues with the shape of my data when creating my dataframe.我在下面的每个列表中看到 5 项,但在创建 dataframe 时,我似乎遇到了数据形状问题。 Any idea why this error is happening?知道为什么会发生此错误吗?

Code:代码:

b = {'Link':links, 'Tax':taxes, 'Description':descrip}
bet = pd.DataFrame(b)
['http://www.redfin.com/IL/Chicago/195-N-Harbor-Dr-60601/unit-509/home/14093313', 'http://www.redfin.com/IL/Chicago/1235-N-Astor-St-60610/unit-3N/home/13054822', 'http://www.redfin.com/MO/St-Louis/2622-S-11th-St-63118/home/93686930', 'http://www.redfin.com/IL/Chicago/426-W-Barry-Ave-60657/unit-408/home/13373863', 'http://www.redfin.com/IL/Chicago/310-S-Michigan-Ave-60604/unit-1608/home/45513284']
['$631', '$859', '$377', '$201', '$575']
["Valet parking included! Floor to ceiling bay windows with spectacular views of Navy Pier and Lake Michigan. Open kitchen features newer stainless steel appliances, granite countertops and new dishwasher and washer/dryer. Wood floor in living room and den, nice designer sliding door for second bedroom. Both bathrooms are remodeled recently. Fantastic location, steps away from Millennium Park, Navy Pier, DuSable Harbor, beautiful lakeshore east park, bike paths, Gems World Academy, restaurants, Mariano's, museums, theatre district and more. Many amenities in building include rooftop pool, jacuzzi, fitness center, two party rooms (56th floor library/party room with postcard views of lake), tennis court, grilling and pet relief areas. Perfect for in town, investment or home!", 'Very easy and safe to show! Sellers are highly motivated and can close quickly. Very best value in the building and in the neighborhood. Charming and sophisticated 4-bedroom plus den 2.1 bath penthouse in Gold Coast on quiet and wonderful tree lined Astor Street. Much larger square footage and more rooms than many other comparatively priced co-ops. Small and beautiful vintage elevator building (only 9 units) that is very well cared for and cleaned daily by two person live- in engineering/custodial team. Staff is full service to the building. Unit is very wide and bright with four exposures. Large rooms that flow-great for entertaining in the front-- and privacy and quiet in the back-- perfect for live-in, family visits and could be wonderful multiple "work from home" offices. Hardwood floors, gas fireplace, eat in Poggenpohl kitchen, cozy den, laundry in unit, ADT security system and great storage. Charming and bright sunroom with views down Astor. 50% financing allowed. Healthy reserve fund and no major capital projects planned. Ample space to spread out in this home. One dog or one cat allowed. Assessments are very reasonable and include real estate taxes. Building has an exercise room, kids\' playroom, storage room, bike room and two separate private patios. Patios have garden setting, along with seating and grills. Exterior garden has won many awards from the Gold Coast Neighbors Association. Healthy association and well maintained building. Steps to Lake Shore Drive, Beach, the Red Line train, Oak Street shopping, Northwestern Memorial Hospital, Michigan Ave walking distance to Latin School, Walter Payton, Xavier Warde, St Chrysostom\'s, Lincoln Park High School, Ogden Elementary School district. Multiple parking options right nearby. Assessment is as follows: $2,502.40 (Base Assmt. ) + $1,837.60 (2018 RE Taxes)= $4,340 (Total Assmt. ).', 'When you decide to leave your backyard oasis that includes hot tub, pool & multiple decks, you won’t lose your parking spot ! 2 car garage & garage space for TWO golf carts is yours! Dining & drinks are mere steps away. This renovated (2010) historic Founder Home is three stories; entering into a first floor parlor/dining and kitchen w/½ bath; mudroom off the kitchen;2nd flr master suite w/ walk in closet with own private deck, walk in jacuzzi tub & separate shower; full bath on 3rd flr for the 2 extra bds - perfect for your guests for Mardi Gras and brewery tours! Let’s talk about the (finished) basement – there’s indoor and exterior access, and it’s just begging to be your own pub! Plumbed wetbar & .5 bath – separate office area or storage w/2nd walk out. Oh yeah… there’s 2nd floor laundry too! It’s checking all the right boxes! Here’s some of the boring details that are important: approx. 2200 sq ft plus, new roof: 2020, zoned hvac, working gas fireplace, gas line run to grill.', "PERFECT EAST LAKEVIEW LOCATION IN ELEVATOR BUILDING. Own your own private condo for less than the cost of renting! Fantastic location near the lakefront, park, Belmont and Diversey Harbor, LAC, Mariano's, Broadway and Clark Street shopping and dining, Trader Joes, Belmont El + bus lines, LSD, etc. Welcome home to this condo located on a beautiful tree-lined street. Completely move-in ready, not a thing to do! This home has been meticulously maintained and features custom window coverings, USB outlets and updated lighting. Additional storage included. The building offers common laundry (get it all done in one hour) and secured entry. Rental parking available or street parking. No rental cap! Click on the 3D tour and take a walk around before booking your private showing.", 'Prime location in the heart of the Loop. Corner 3 BD 2BA or 2BD + den split floor plan w/ balcony. Hardwood floors in living space and kitchen with carpet in bedrooms and window coverings throughout. Open kitchen w/ granite, stainless steel appliances, island, breakfast bar etc. Master bedroom suite has large walk in closet and master bath w/ radiant heated floor, soaking tub, double vanity sinks and separate glass shower. Large storage on same floor! Wonderful amenities including fitness center, whirlpool, sauna, outdoor terrace and 24 hr doorman. Parking additional $35,000. Grant Park, steps to Millennium Park, Art Institute and more!']

Error:错误:

Traceback (most recent call last):
  File "C:\Users\Python\Price Tracking\Real Estate\RealEstate-Scraping.py", line 79, in <module>
    bet = pd.DataFrame(b, index=[1])
  File "C:\Python38\lib\site-packages\pandas\core\frame.py", line 435, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "C:\Python38\lib\site-packages\pandas\core\internals\construction.py", line 254, in init_dict
    return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "C:\Python38\lib\site-packages\pandas\core\internals\construction.py", line 74, in arrays_to_mgr
    return create_block_manager_from_arrays(arrays, arr_names, axes)
  File "C:\Python38\lib\site-packages\pandas\core\internals\managers.py", line 1675, in create_block_manager_from_arrays
    construction_error(len(arrays), arrays[0].shape, axes, e)
  File "C:\Python38\lib\site-packages\pandas\core\internals\managers.py", line 1694, in construction_error
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (5, 3), indices imply (1, 3)

The code you wrote works for me你写的代码对我有用

links = ['http://www.redfin.com/IL/Chicago/195-N-Harbor-Dr-60601/unit-509/home/14093313', 'http://www.redfin.com/IL/Chicago/1235-N-Astor-St-60610/unit-3N/home/13054822', 'http://www.redfin.com/MO/St-Louis/2622-S-11th-St-63118/home/93686930', 'http://www.redfin.com/IL/Chicago/426-W-Barry-Ave-60657/unit-408/home/13373863', 'http://www.redfin.com/IL/Chicago/310-S-Michigan-Ave-60604/unit-1608/home/45513284']
taxes = ['$631', '$859', '$377', '$201', '$575']
descrip = ["Valet parking included! Floor to ceiling bay windows with spectacular views of Navy Pier and Lake Michigan. Open kitchen features newer stainless steel appliances, granite countertops and new dishwasher and washer/dryer. Wood floor in living room and den, nice designer sliding door for second bedroom. Both bathrooms are remodeled recently. Fantastic location, steps away from Millennium Park, Navy Pier, DuSable Harbor, beautiful lakeshore east park, bike paths, Gems World Academy, restaurants, Mariano's, museums, theatre district and more. Many amenities in building include rooftop pool, jacuzzi, fitness center, two party rooms (56th floor library/party room with postcard views of lake), tennis court, grilling and pet relief areas. Perfect for in town, investment or home!", 'Very easy and safe to show! Sellers are highly motivated and can close quickly. Very best value in the building and in the neighborhood. Charming and sophisticated 4-bedroom plus den 2.1 bath penthouse in Gold Coast on quiet and wonderful tree lined Astor Street. Much larger square footage and more rooms than many other comparatively priced co-ops. Small and beautiful vintage elevator building (only 9 units) that is very well cared for and cleaned daily by two person live- in engineering/custodial team. Staff is full service to the building. Unit is very wide and bright with four exposures. Large rooms that flow-great for entertaining in the front-- and privacy and quiet in the back-- perfect for live-in, family visits and could be wonderful multiple "work from home" offices. Hardwood floors, gas fireplace, eat in Poggenpohl kitchen, cozy den, laundry in unit, ADT security system and great storage. Charming and bright sunroom with views down Astor. 50% financing allowed. Healthy reserve fund and no major capital projects planned. Ample space to spread out in this home. One dog or one cat allowed. Assessments are very reasonable and include real estate taxes. Building has an exercise room, kids\' playroom, storage room, bike room and two separate private patios. Patios have garden setting, along with seating and grills. Exterior garden has won many awards from the Gold Coast Neighbors Association. Healthy association and well maintained building. Steps to Lake Shore Drive, Beach, the Red Line train, Oak Street shopping, Northwestern Memorial Hospital, Michigan Ave walking distance to Latin School, Walter Payton, Xavier Warde, St Chrysostom\'s, Lincoln Park High School, Ogden Elementary School district. Multiple parking options right nearby. Assessment is as follows: $2,502.40 (Base Assmt. ) + $1,837.60 (2018 RE Taxes)= $4,340 (Total Assmt. ).', 'When you decide to leave your backyard oasis that includes hot tub, pool & multiple decks, you won’t lose your parking spot ! 2 car garage & garage space for TWO golf carts is yours! Dining & drinks are mere steps away. This renovated (2010) historic Founder Home is three stories; entering into a first floor parlor/dining and kitchen w/½ bath; mudroom off the kitchen;2nd flr master suite w/ walk in closet with own private deck, walk in jacuzzi tub & separate shower; full bath on 3rd flr for the 2 extra bds - perfect for your guests for Mardi Gras and brewery tours! Let’s talk about the (finished) basement – there’s indoor and exterior access, and it’s just begging to be your own pub! Plumbed wetbar & .5 bath – separate office area or storage w/2nd walk out. Oh yeah… there’s 2nd floor laundry too! It’s checking all the right boxes! Here’s some of the boring details that are important: approx. 2200 sq ft plus, new roof: 2020, zoned hvac, working gas fireplace, gas line run to grill.', "PERFECT EAST LAKEVIEW LOCATION IN ELEVATOR BUILDING. Own your own private condo for less than the cost of renting! Fantastic location near the lakefront, park, Belmont and Diversey Harbor, LAC, Mariano's, Broadway and Clark Street shopping and dining, Trader Joes, Belmont El + bus lines, LSD, etc. Welcome home to this condo located on a beautiful tree-lined street. Completely move-in ready, not a thing to do! This home has been meticulously maintained and features custom window coverings, USB outlets and updated lighting. Additional storage included. The building offers common laundry (get it all done in one hour) and secured entry. Rental parking available or street parking. No rental cap! Click on the 3D tour and take a walk around before booking your private showing.", 'Prime location in the heart of the Loop. Corner 3 BD 2BA or 2BD + den split floor plan w/ balcony. Hardwood floors in living space and kitchen with carpet in bedrooms and window coverings throughout. Open kitchen w/ granite, stainless steel appliances, island, breakfast bar etc. Master bedroom suite has large walk in closet and master bath w/ radiant heated floor, soaking tub, double vanity sinks and separate glass shower. Large storage on same floor! Wonderful amenities including fitness center, whirlpool, sauna, outdoor terrace and 24 hr doorman. Parking additional $35,000. Grant Park, steps to Millennium Park, Art Institute and more!']
b = {'Link':links, 'Tax':taxes, 'Description':descrip}
bet = pd.DataFrame(b)

It creates the DataFrame for me.它为我创建了 DataFrame。

I don't understand what your problem exactly is, but if you just want to make a list into a dataframe, you can do this.我不明白你的问题到底是什么,但如果你只是想把一个列表变成 dataframe,你可以这样做。

import pandas as pd

links = ['http://www.redfin.com/IL/Chicago/195-N-Harbor-Dr-60601/unit-509/home/14093313', 'http://www.redfin.com/IL/Chicago/1235-N-Astor-St-60610/unit-3N/home/13054822', 'http://www.redfin.com/MO/St-Louis/2622-S-11th-St-63118/home/93686930', 'http://www.redfin.com/IL/Chicago/426-W-Barry-Ave-60657/unit-408/home/13373863', 'http://www.redfin.com/IL/Chicago/310-S-Michigan-Ave-60604/unit-1608/home/45513284']
taxes = ['$631', '$859', '$377', '$201', '$575']
descrip = ['descript1', 'descript2', 'descript3', 'descript4', 'descript5']

b = {'Link':links, 'Tax':taxes, 'Description':descrip}
bet = pd.DataFrame(b)

if your problem is regarding this line of code inside of your error,如果您的问题与错误中的这行代码有关,

bet = pd.DataFrame(b, index=[1])

then you can change that into this然后你可以把它改成这个

bet = pd.DataFrame([b], index=[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM