pandas 如何按列减少熊猫数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31872693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I columnwise reduce a pandas dataframe?
提问by The Unfun Cat
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?
任何替代方案?
采纳答案by BushMinusZero
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.
虽然可能有其他路线。

