2012-10-05 9 views
23

私は同じように、DFのいずれかのカラムに.map(func)を使用することができます。私もパンダデータフレーム:すべての列に関数を適用

df=DataFrame({'a':[1,2,3,4,5,6],'b':[2,3,4,5,6,7]}) 

df['a']=df['a'].map(lambda x: x > 1) 

できます

df['a'],df['b']=df['a'].map(lambda x: x > 1),df['b'].map(lambda x: x > 1) 

は適用するために、より神託の方法はありますすべての列やフレーム全体(ループなし)の関数?

+0

'ラムダxにご' lambda'簡素化します。x> 1 ' – Blender

+0

@ブレンダー - – root

+0

はちょうどそれを指摘...編集したおかげで、。元の質問を編集する必要はありません。 – Blender

答えて

35

あなたが正しく理解している場合は、applymapメソッドを探しています。以降0.20.0から

>>> print df 
    A B C 
0 -1 0 0 
1 -4 3 -1 
2 -1 0 2 
3 0 3 2 
4 1 -1 0 
>>> print df.applymap(lambda x: x>1) 
     A  B  C 
0 False False False 
1 False True False 
2 False False True 
3 False True True 
4 False False False 
+0

@ BrenBarn - これはまさに私が探していたものです。ドキュメントからそれを気付かなかった。ありがとう。 – root

1

、あなたはtransform

In [578]: df.transform(lambda x: x > 1) 
Out[578]: 
     A  B  C 
0 False False False 
1 False True False 
2 False False True 
3 False True True 
4 False False False 

In [579]: df 
Out[579]: 
    A B C 
0 -1 0 0 
1 -4 3 -1 
2 -1 0 2 
3 0 3 2 
4 1 -1 0 

を使用することができますそして、この単純化した場合のために、なぜちょうどdf > 1を使用していませんか?

In [582]: df > 1 
Out[582]: 
     A  B  C 
0 False False False 
1 False True False 
2 False False True 
3 False True True 
4 False False False 
関連する問題