Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame: apply function to all columns

I can use .map(func) on any column in a df, like:

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) 

I could also:

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

Is there a more pythonic way to apply a function to all columns or the entire frame (without a loop)?

like image 434
root Avatar asked Oct 05 '12 06:10

root


People also ask

How do I apply a function to all columns in pandas?

Use apply() to Apply Functions to Columns in Pandas The apply() method allows to apply a function for a whole DataFrame, either across columns or rows. We set the parameter axis as 0 for rows and 1 for columns. The new appended e column is the sum of data in column a and b .

How do I apply a function to all columns?

Just select the cell F2, place the cursor on the bottom right corner, hold and drag the Fill handle to apply the formula to the entire column in all adjacent cells.

What is apply () in pandas?

The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.


2 Answers

If I understand you right, you're looking for the applymap method.

>>> 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 
like image 84
BrenBarn Avatar answered Sep 19 '22 03:09

BrenBarn


From 0.20.0 onwards, you can use 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 

And, for this simplistic case, why not just use 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 
like image 39
Zero Avatar answered Sep 21 '22 03:09

Zero