当我将一个函数应用于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
发布于 2020-04-03 04:00:19
此警告是在discussion on GitHub之后的pandas 1.0.0中引入的。所以最好使用这里建议的:
df.groupby([0, 1])[[1, 2]].apply(sum)
也可以将切片操作移到最后,但效率不高:
df.groupby([0, 1]).apply(sum).loc[:, 1:]
感谢@ALollz和@cmosig的有用评论。
发布于 2020-06-18 23:39:57
在groupby方法后使用双括号。单括号用于输出熊猫系列,双括号用于输出熊猫DataFrame。
df.groupby([0,1])[[1,2]].apply(sum)
https://stackoverflow.com/questions/60999753
复制相似问题