minerals minerals - 2 months ago 11
Python Question

How to convert Series into DataFrame preserving the columns?

I have a large

DataFrame
with the following contents:

obj0 type obj1 counts
car P wheel 2
tree P branch 5
box I ball 23
wheel P bus 3
grass A cow 10
...


I want to see how many
obj0
and
type
pairs I have, I do:
obj0_sums = pdf.groupby(['obj0', 'type'])['counts'].sum()
.

obj0 type
car P 5
tree P 15
box I 50
...


Then I need to do other operations on
obj0_sums
later but it is a
Series
where
obj0
and
type
columns have turned into the first row. How can I make it stay a
DataFrame
and preserve columns after
groupby
operation?

Answer

try this:

In [262]: df.groupby(['obj0', 'type'], as_index=False)['counts'].sum()
Out[262]:
    obj0 type  counts
0    box    I      23
1    car    P       2
2  grass    A      10
3   tree    P       5
4  wheel    P       3

from docs:

as_index : boolean, default True

For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output

Comments