ブール規則を使用してラムダ関数を適用する 1 つの方法を次に示します。
In [58]: df
Out[58]:
id a b c d
0 1 0 -1 1 1
1 42 0 1 0 0
2 128 1 -1 0 1
In [59]: cols = list('abcd')
In [60]: (df[cols] > 0).apply(lambda x: ' '.join(x[x].index), axis=1)
Out[60]:
0 c d
1 b
2 a d
dtype: object
結果をに割り当てることができますdf['one_entries'] =
適用機能の詳細。
最初の行を取ります。
In [83]: x = df[cols].ix[0] > 0
In [84]: x
Out[84]:
a False
b False
c True
d True
Name: 0, dtype: bool
x
行のブール値、ゼロより大きい値を提供します。x[x]
のみを返しTrue
ます。基本的に、列名をインデックスとするシリーズです。
In [85]: x[x]
Out[85]:
c True
d True
Name: 0, dtype: bool
x[x].index
列名を提供します。
In [86]: x[x].index
Out[86]: Index([u'c', u'd'], dtype='object')