python pandas报错AttributeError: ‘DataFrame‘ object has no attribute ‘append‘

搜了一圈,都是这种答案,不用估计,就是弃用了。

DataFrame.append在1.4版本中弃用,2.0版本中完全删除。

为啥?

因为它有严重的性能问题,有时候还会出现性能警告

DataFrame is highly fragmented.  
This is usually the result of calling `frame.insert` many times, 
which has poor performance. 
Consider joining all columns at once using pd.concat(axis=1) instead.  
To get a de-fragmented frame, use `newframe = frame.copy()`

在此前的pandas文档中有这么一段话

It is worth noting however, that concat (and therefore append) makes a full copy of the data, 
and that constantly reusing this function can create a significant performance hit.
 If you need to use the operation over several datasets, use a list comprehension.

翻译过来就是说append操作会生成数据的完整副本,反复重用此函数可能会对性能造成重大影响。

一般来说,append都是在迭代中使用较多。在官方讨论看到说DataFrame.append is around the 10th most visited page in our API docs.

可见,这个方法使用量是相当大的。

比如Brideau这位老哥有一个几百万记录的数据集,使用如下脚本进行处理,肯定是贼慢的

city = ["SomeCity"]
df = DataFrame({}, columns=['Date', 'HouseID', 'Price'])
for city in cities:
    for dateRun in data[city]:
        for record in data[city][dateRun]:
            recSeries = Series([record['Timestamp'], 
                                record['Id'], 
                                record['Price']],
                                index = ['Date', 'HouseID', 'Price'])
            FredDF = FredDF.append(recSeries, ignore_index=True)

也有人做过基准测试,使用append和loc都挺慢的。

官方推荐:Deprecated since version 1.4.0: Use concat() instead. 

但更好的做法是

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值