使用布尔检查返回Dataframe中的列名

时间:2017-08-31 06:32:33

标签: pandas boolean python-3.6

我有以下带有布尔值的Dataframe

Out[25]: 
                0     1      2
Date                          
2007-01-03  False  True  False
2007-01-04  False  False True
2007-01-05  False  True  False
2007-01-08  True   False False
2007-01-09  False  True  False

我希望得到一个DF,它返回每行的列值'True'的列索引。

必需输出:

            0
Date                          
2007-01-03  1
2007-01-04  2
2007-01-05  1
2007-01-08  0
2007-01-09  1

最好的pythonic方法是什么?

1 个答案:

答案 0 :(得分:7)

如果每行只有一个True使用idxmax

df['new'] = df.idxmax(axis=1)
print (df)
                0      1      2 new
Date                               
2007-01-03  False   True  False   1
2007-01-04  False  False   True   2
2007-01-05  False   True  False   1
2007-01-08   True  False  False   0
2007-01-09  False   True  False   1

如果多个True s:

df['new'] = df.apply(lambda x: ','.join(x.index[x]), axis=1)
print (df)
                0      1      2  new
Date                                
2007-01-03  False   True   True  1,2
2007-01-04  False  False   True    2
2007-01-05  False   True  False    1
2007-01-08   True  False  False    0
2007-01-09  False   True  False    1

另一种解决方案:

print (['{}, '.format(x) for x in df.columns])
['0, ', '1, ', '2, ']

s = np.where(df, ['{}, '.format(x) for x in df.columns], '')
df['new'] = pd.Series([''.join(x).strip(', ') for x in s], index=df.index)
print (df)
                0      1      2   new
Date                                 
2007-01-03  False   True   True  1, 2
2007-01-04  False  False   True     2
2007-01-05  False   True  False     1
2007-01-08   True  False  False     0
2007-01-09  False   True  False     1