pandas 熊猫追加不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51997818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Append Not Working
提问by Lakshmi Bhavani - Intel
datum = soup.findAll('a', {'class': 'result-title'})
for data in datum:
print(data.text)
print(data.get('href'))
df = {'Title': data.text, 'Url': data.get('href')}
houseitems.append(df, ignore_index=True)
What is wrong with my Code? Why when I asked for my houseitems, it gives me empty data.
我的代码有什么问题?为什么当我询问我的家居用品时,它给了我空数据。
Empty DataFrame
空数据帧
Columns: [Title, Url, Price]
Index: []
采纳答案by jezrael
Problem is you need assign back appended DataFrame
, because pandas DataFrame.append
NOTworking inplace like pure python append
.
问题是你需要分配回追加DataFrame
,因为Pandas不是工作就地喜欢纯Python 。DataFrame.append
append
It seem you want append to list
, so parameter ignore_index=True
is not necessary:
似乎您想附加到list
,因此ignore_index=True
不需要参数:
Loop solution:
循环解决方案:
houseitems = []
for data in datum:
print(data.text)
print(data.get('href'))
df = {'Title': data.text, 'Url': data.get('href')}
houseitems.append(df)
Or list comprehension
solution:
或者list comprehension
解决办法:
houseitems = [{'Title': data.text, 'Url': data.get('href')} for data in datum]
And then create DataFrame
:
然后创建DataFrame
:
df1 = pd.DataFrame(houseitems)
回答by Lakshmi Bhavani - Intel
Try modify line in your code
尝试修改代码中的行
houseitems.append(df, ignore_index=True)
as
作为
houseitems=houseitems.append(df, ignore_index=True)