当我对groupby对象的多个列应用函数时,Pandas会发出“ future 警告”。它建议使用列表作为索引而不是元组。怎么会这样呢?
>>> 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
最佳答案
此警告是在discussion on GitHub之后在 Pandas 1.0.0中引入的。因此,最好使用那里的建议:
df.groupby([0, 1])[[1, 2]].apply(sum)
也可以将 slice 操作移到末尾,但是效率不高:df.groupby([0, 1]).apply(sum).loc[:, 1:]
感谢@ALollz和@cmosig提供有用的评论。
关于python - Pandas , future 警告:使用多个键进行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60999753/