[pandas] flatten dataframe?

902 views
Skip to first unread message

Angus McMorland

unread,
Jan 27, 2012, 11:01:37 AM1/27/12
to pystat...@googlegroups.com
Hi all,

I have a need to flatten a small DataFrame into a Series, so that it
can be inserted into a larger DataFrame that is accumulating the
results of my computations (using `combine_first`). Is there a potted
function around that I haven't discovered that can do this for me? I'm
imagining something that would convert

a b
0 9 4
1 2 0
2 5 9

to something like

a-0 a-1 a-2 b-0 b-1 b-2
9 2 5 4 0 9

Many thanks,

Angus
--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh

Wes McKinney

unread,
Jan 27, 2012, 11:09:52 AM1/27/12
to pystat...@googlegroups.com

How about this:


In [17]: df
Out[17]:


a b
0 9 4
1 2 0
2 5 9

In [18]: df.unstack().rename(lambda x: '%s-%d' % x)
Out[18]:
a-0 9
a-1 2
a-2 5
b-0 4
b-1 0
b-2 9

(remember combine_first does not add, it just take the left value if
it's non-NA otherwise the right value)

Angus McMorland

unread,
Jan 27, 2012, 12:35:56 PM1/27/12
to pystat...@googlegroups.com

That's perfect.

> (remember combine_first does not add, it just take the left value if
> it's non-NA otherwise the right value)

Got it, thanks. That is the behaviour I need in this case: I have a
moderately computationally intensive operation, from which I'm
generating a small dataframe output (right value), and then placing
those results in a larger table (left value), for which the columns
being replaced are all NA.

Thanks again,

Reply all
Reply to author
Forward
0 new messages