Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe - change values based on column heading

Tags:

python

pandas

I have a dataframe that looks like the following:

In [74]: data2

Out[74]: 
            a  b  c

2012-06-12  0  1  1
2012-06-13  1  1  0
2012-06-14  1  0  1
2012-06-15  1  0  1
2012-06-16  1  1  0
2012-06-17  1  0  1

Is there a way to make the values = the column heading where the value = 1?

Result df:

            a  b  c

2012-06-12  0  b  c
2012-06-13  a  b  0
2012-06-14  a  0  c
2012-06-15  a  0  c
2012-06-16  a  b  0
2012-06-17  a  0  c

And then remove the values that = 0 such that the df reduces to 2 columns: (column heading is not relevant at this point)

Result df:

            1  2  
2012-06-12  c  b  
2012-06-13  a  b  
2012-06-14  a  c  
2012-06-15  a  c  
2012-06-16  a  b  
2012-06-17  a  c  
like image 301
Tony Avatar asked Aug 12 '12 08:08

Tony


2 Answers

You can also summon some deeper pandas-fu and do:

In [28]: df.apply(lambda x: x.astype(object).replace(1, x.name))
Out[28]: 
            a  b  c
2012-06-12  0  b  c
2012-06-13  a  b  0
2012-06-14  a  0  c
2012-06-15  a  0  c
2012-06-16  a  b  0
2012-06-17  a  0  c
like image 58
Wes McKinney Avatar answered Sep 21 '22 10:09

Wes McKinney


from pandas import *
df = DataFrame([[0, 1, 1], [1, 1, 0], [1, 0, 1],], columns=['a','b','c'])

foo = []
for i in df.index:
    foo.append( df.columns[df.ix[i] == 1])
DataFrame(foo, index = df.index)

Which returns:

   0  1
0  b  c
1  a  b
2  a  c
like image 30
jrouquie Avatar answered Sep 22 '22 10:09

jrouquie