Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I columnwise reduce a pandas dataframe?

Tags:

python

pandas

I have a dataframe as shown below:

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO
    from functools import reduce

import pandas as pd
from numpy import uint8, logical_or

df = pd.read_table(StringIO("""a    b    c
1   0   0
1   1   1
0   1   1
1   1   0"""), sep="\s+", dtype=uint8, header=0)

How do I columnwise reduce the dataframe?

Currently I just put all the vectors in a list and reduce it, but this cannot be the most pandastic way of doing it:

gene_vectors = [df[v] for v in df]

print(reduce(logical_or, gene_vectors))

Any alternatives?

like image 336
The Unfun Cat Avatar asked Aug 07 '15 08:08

The Unfun Cat


1 Answers

I believe this is the "pandastic" way to achieve this

df.apply(lambda x: reduce(logical_or,x), axis=1)

although there might be other routes.

like image 181
BushMinusZero Avatar answered Oct 09 '22 08:10

BushMinusZero