Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas, Future Warning: Indexing with multiple keys

Tags:

python

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 
like image 787
cmosig Avatar asked Apr 02 '20 19:04

cmosig


People also ask

Can DataFrame have multiple indexes?

You can also construct a MultiIndex from a DataFrame directly, using the method MultiIndex. from_frame() . This is a complementary method to MultiIndex.


1 Answers

This warning was introduced in pandas 1.0.0, following a discussion on GitHub. So best use what was suggested there:

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

It's also possible to move the slicing operation to the end, but that is not as efficient:

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

Thanks @ALollz and @cmosig for helpful comments.

like image 149
Arne Avatar answered Sep 21 '22 04:09

Arne