Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function on Pandas dataframe

Tags:

python

pandas

I'm a newbie to pandas dataframe, and I wanted to apply a function to each column so that it computes for each element x, x/max of column.

I referenced this question, but am having trouble accessing the maximum of each column. Thanks in advance Pandas DataFrame: apply function to all columns

Input:

      A  B  C  D
   0  8  3  5  8
   1  9  4  0  4
   2  5  4  3  8
   3  4  8  5  1

Output:

      A     B     C    D
   0  8/9  3/8  5/5  8/8
   1  9/9  4/8  0/5  4/8
   2  5/9  4/8  3/5  8/8
   3  4/9  8/8  5/5  1/8
like image 492
msunbot Avatar asked Dec 18 '12 17:12

msunbot


People also ask

How do you apply a function in a data frame?

DataFrame - apply() function. The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

How do I apply a function in pandas?

DataFrame applymap() function If you want to apply a function element-wise, you can use applymap() function. This function doesn't have additional arguments. The function is applied to each of the element and the returned value is used to create the result DataFrame object.

How do you apply a function to a DataFrame row?

Use apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply(). By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.

What is apply () in Python?

apply() method. This function acts as a map() function in Python. It takes a function as an input and applies this function to an entire DataFrame. If you are working with tabular data, you must specify an axis you want your function to act on ( 0 for columns; and 1 for rows).


1 Answers

Something like this should work:

>>> from pandas import DataFrame
>>> 
>>> df = DataFrame({"A": [8,9,5,4], "B": [3,4,4,8], "C": [5,0,3,5], "D": [8,4,8,1]})
>>> df.max()
A    9
B    8
C    5
D    8
>>> (df * 1.0)/df.max()
          A      B    C      D
0  0.888889  0.375  1.0  1.000
1  1.000000  0.500  0.0  0.500
2  0.555556  0.500  0.6  1.000
3  0.444444  1.000  1.0  0.125

Note that I multiplied df by 1.0 so that it didn't consists of ints anymore (.astype(float) would have worked too) to avoid integer division and a resulting DataFrame full of 0s and 1s.

like image 51
DSM Avatar answered Oct 19 '22 18:10

DSM