Pandas pd not populating with .append



我已经用Slenium&蟒蛇dict的输出如下:

data = {'Price': '£1,995', 'Title': 'BMW X3', 'Key Specs': ['2006 (56 reg)', 'SUV', '110,000 miles', '2.0L', '150BHP', 'Manual', 'Diesel'], 'Year': '2006 (56 reg)', 'Type': 'SUV', 'Milage': '110,000 miles', 'Displacement': '2.0L', 'Power': '150BHP', 'Transmission': 'Manual', 'Fuel': 'Diesel'}

我在脚本的开头创建了一个名为"df"的Pandas DataFrame,如下所示:

df = pd.DataFrame(columns=['Title', 'Price', 'Year', 'Type', 'Milage', 'Displacement', 'Power', 'Transmission', 'Fuel', 'Owner Count', 'Key Specs', 'Attention Grabber', 'URL'])

遗憾的是,由于某些原因,我没有成功地将data附加到我的df。通过以下线路(在环路内(:

df = df.append(data, ignore_index=True)

你能帮忙吗?

错误消息如下:

Empty DataFrame
Columns: [Title, Price, Year, Type, Milage, Displacement, Power, Transmission, Fuel, Owner Count, Key Specs, Attention Grabber, URL]
Index: []

我不能确切地说明为什么带有列的空DataFrame不能追加一行。但是,如果您将同一行附加到常规的空DataFrame,它可以很好地工作:

df = pd.DataFrame()
df = df.append(data, ignore_index=True)

您可以随时添加数据丢失的列。

无论如何,出于性能原因,我强烈建议不要逐行附加数据。对于每个这样的调用,pandas创建DataFrame的新副本。如果创建一个包含所有要附加的数据的列表,然后调用append一次,效率会更高。

问题出在代码的这一部分。这打破了你的循环

try: 
data['URL'] = listing.find_elements_by_css_selector('a.js-click-handler listing-fpa-link tracking-standard-link')[0].get_attribute('href')
except:
break

因此,这不会被执行,并且最终会得到一个空的数据帧。

df = pd.DataFrame(data, columns=['Title', 'Price', 'Year', 'Type', 'Milage', 'Displacement', 'Power', 'Transmission', 'Fuel', 'Owner Count', 'Key Specs', 'Attention Grabber', 'URL'])

此外,由于dict中包含一个列表元素,您需要这样做,否则您将为"密钥规范"列表中的每个元素获得一个新记录。

df = pd.DataFrame([data], columns=['Title', 'Price', 'Year', 'Type', 'Milage', 'Displacement', 'Power', 'Transmission', 'Fuel', 'Owner Count', 'Key Specs', 'Attention Grabber', 'URL'])

另一个解决方案是

num = len(df) + 1
df.loc[num] = data

您已经提到,您正在将df = df.append(data, ignore_index=True)放置在一个循环中

像下面这样尝试,不要有任何循环。

import pandas as pd
data = {'Price': '£1,995', 'Title': 'BMW X3', 'Key Specs': ['2006 (56 reg)', 'SUV', '110,000 miles', '2.0L', '150BHP', 'Manual', 'Diesel'], 'Year': '2006 (56 reg)', 'Type': 'SUV', 'Milage': '110,000 miles', 'Displacement': '2.0L', 'Power': '150BHP', 'Transmission': 'Manual', 'Fuel': 'Diesel'}
df = pd.DataFrame(columns=['Title', 'Price', 'Year', 'Type', 'Milage', 'Displacement', 'Power', 'Transmission', 'Fuel', 'Owner Count', 'Key Specs', 'Attention Grabber', 'URL'])
df = df.append(data, ignore_index=True)
print(df)

考虑到数据列表作为输入,下面的代码片段会有所帮助。

import pandas as pd
df = pd.DataFrame(columns=['Title', 'Price', 'Year', 'Type', 'Milage', 'Displacement', 'Power', 'Transmission', 'Fuel', 'Owner Count', 'Key Specs', 'Attention Grabber', 'URL'])
# make your input as list
data = [{'Price': '£1,995', 
'Title': 'BMW X3', 
'Key Specs': ['2006 (56 reg)', 'SUV', '110,000 miles', '2.0L', '150BHP', 'Manual', 'Diesel'], 
'Year': '2006 (56 reg)', 
'Type': 'SUV', 
'Milage': '110,000 miles', 
'Displacement': '2.0L', 
'Power': '150BHP', 
'Transmission': 'Manual', 
'Fuel': 'Diesel'},

{'Price': '£2,995', 
'Title': 'BMW X7', 
'Key Specs': ['2016 (66 reg)', 'SUV', '210,000 miles', '4.0L', '250BHP', 'Automatic', 'Diesel'], 
'Year': '2016 (66 reg)', 
'Type': 'SUV', 
'Milage': '210,000 miles', 
'Displacement': '4.0L', 
'Power': '250BHP', 
'Transmission': 'Manual', 
'Fuel': 'Diesel'}]

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

我想明白了。我从DataFrame中删除了已定义的列,只留下那些正在调用的列。这就成功了,现在我的原始代码可以工作了。感谢所有有用的反馈

最新更新