Thursday, 15 April 2010

python - How to get values from a "cell" of a "groupby" object? -



python - How to get values from a "cell" of a "groupby" object? -

assume have next info frame:

b c d 0 foo 1 1 10 1 bar 1 2 20 2 foo 2 3 30 3 bar 1 4 40 4 foo 2 5 50 5 bar 2 6 60 6 foo 1 7 70 7 foo 2 8 80

now can grouping first column: grouped = df.groupby('a'). result next dataframegroupby object:

b c d 0 foo [one,two,two,one,two] [1,3,5,7,8] [10,30,50,70,80] 1 bar [one,one,two] [2,4,6] [20,40,60]

now access values particular cell. how can it? illustration want values column 'd' , row 'a'=='foo' (the first row). in other words want [10,30,50,70,80]. possible?

are thinking of this?

>>> df b c d 0 foo 1 1 10 1 bar 1 2 20 2 foo 2 3 30 3 bar 1 4 40 4 foo 2 5 50 5 bar 2 6 60 6 foo 1 7 70 7 foo 2 8 80 >>> df.groupby("a").get_group("foo")["d"] 0 10 2 30 4 50 6 70 7 80 name: d >>> df.groupby("a").get_group("foo")["d"].tolist() [10, 30, 50, 70, 80]

python group-by pandas

No comments:

Post a Comment