pandas ,未来警告:使用多个键进行索引 [英] Pandas, Future Warning: Indexing with multiple keys

查看:269
本文介绍了 pandas ,未来警告:使用多个键进行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个函数应用于groupby对象的多列时,Pandas会发出未来警告".它建议使用列表作为索引而不是元组.怎么会这样呢?

Pandas throws a Future Warning when I apply a function to multiple columns of a groupby object. It suggests to use a list as index instead of tuples. How would one go about this?

>>> df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])
>>> df.groupby([0,1])[1,2].apply(sum)
<stdin>:1: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
     1  2
0 1      
1 2  2  3
4 5  5  6
7 8  8  9

推荐答案

编辑:@ALollz是正确的,顺序不是问题.在熊猫1.0.0中此处引入了警告.所以最好使用他们的建议:

@ALollz is right, the order was not the issue. The warning was introduced here in pandas 1.0.0 . So best use what they suggested:

df.groupby([0,1])[[1, 2]].apply(sum)

按照@cmosig的说明,将切片操作按如下所示进行到底效率不高.

Moving the slicing operation to the end as follows is not as efficient, as pointed out by @cmosig.

df.groupby([0,1]).apply(sum).loc[:, 1:]

这篇关于 pandas ,未来警告:使用多个键进行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆