Pandas,未来警告:使用多个键进行索引

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

>>> 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

回答:

在GitHub上进行讨论后,此警告在pandas 1.0.0中引入。因此,最好使用那里的建议:

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

也可以将切片操作移到末尾,但是效率不高:

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

感谢@ALollz和@cmosig提供有用的评论。

以上是 Pandas,未来警告:使用多个键进行索引 的全部内容, 来源链接: utcz.com/qa/427319.html

回到顶部