Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "group by" cell value in pandas?

Tags:

python

pandas

I have a DataFrame which look like:

_|a |b |c
x|1 |1 |1
y|2 |2 |3
z|3 |2 |1

I want the result to be:

{
    1: [(x,a),(x,b),(x,c),(z,c)}
    2: [(y,a),(y,b),(z,b)]
    3: [(y,c),(z,a)]
}

I dont care if the result is a dictionary or another dataframe

like image 650
Netanel Avatar asked Jan 25 '23 13:01

Netanel


1 Answers

You can use GroupBy.groups here

g = df.stack()
g.groupby(g).groups
{
  1: [('x', 'a'), ('x', 'b'), ('x', 'c'), ('z', 'c')], 
  2: [('y', 'a'), ('y', 'b'), ('z', 'b')], 
  3: [('y', 'c'), ('z', 'a')]
}
like image 123
Ch3steR Avatar answered Jan 27 '23 04:01

Ch3steR