2017-03-12 18 views
0

いくつかの整数値を含むデータフレームがあります。複数の列[col1、col3、col4]がすべて0でない場合にのみ、行の新しいデータフレームを作成します。例:LAMBDAを使用したDataFrame複数の列の比較

 col1 col2  col3 col4 col5 col6 
0  0 text1   3  0  22  0 
1  9 text2   13  11  22  1 
2  0 text3   0  0  22  0  # not valid 
3  9 text4   13  11  22  0 
4  0 text5   0  1  12  4 

単一のラムダでこれを実行できるかどうかわかりません。

答えて

1

カスタム機能はまったく必要ありません。私達はちょうど、私たちが望むの列を選択し、当社のブール値の比較を行い、その後、あなたのデータフレームへのインデックスにそれを使用することができます。

In [28]: df[["col1", "col3", "col4"]] == 0 
Out[28]: 
    col1 col3 col4 
0 True False True 
1 False False False 
2 True True True 
3 False False False 
4 True True False 

In [29]: (df[["col1", "col3", "col4"]] == 0).all(axis=1) 
Out[29]: 
0 False 
1 False 
2  True 
3 False 
4 False 
dtype: bool 

In [30]: df.loc[~(df[["col1", "col3", "col4"]] == 0).all(axis=1)] 
Out[30]: 
    col1 col2 col3 col4 col5 col6 
0  0 text1  3  0 22  0 
1  9 text2 13 11 22  1 
3  9 text4 13 11 22  0 
4  0 text5  0  1 12  4 

それを書き換えると同様の方法はたくさんあります(すべてではないゼロは0(ゼロ)以外であること、などであります)

関連する問題