python - Pandas , future 警告:使用多个键进行索引

标签 python pandas

当我对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/

相关文章:

javascript - Flask - 让 JavaScript 和 Python 进行通信

python - 使用 Pinax 启动 Django 时出错

python - 对齐两个字符串 : Bigramm/Buble

python - 大型时间序列数据集上不同大小的 block

python区分数据帧列的 '300'和 '300.0'

Python:Collections.Counter 与 defaultdict(int)

python - 阻止 Sphinx 执行模块

python - 通过 Python 计算两个字符串变量之间的公共(public)条目

python - Pandas DataFrame,将复杂函数智能应用于 groupby 结果

javascript - 如何在 JavaScript 中读取 html 表格列表