Anantha Krishnan S
Anantha Krishnan S

Reputation: 15

How to apply a function to all the columns in a data frame and take output in the form of dataframe in python

I have two functions that do some calculation and gives me results. For now, I am able to apply it in one column and get the result in the form of a dataframe. I need to know how I can apply the function on all the columns in the dataframe and get results as well in the form of a dataframe.

Say I have a data frame as below and I need to apply the function on each column in the data frame and get a dataframe with results corresponding for all the columns.

A   B   C   D   E   F
1456    6744    9876    374 65413   1456
654 2314    674654  2156    872 6744
875 653 36541   345 4963    9876
6875    7401    3654    465 3547    374
78654   8662    35  6987    6874    65413
658 94512   687 489 8756    5854

Results

A   B   C   D   E   F
2110    9058    684530  2530    66285   8200
1529    2967    711195  2501    5835    16620
7750    8054    40195   810 8510    10250
85529   16063   3689    7452    10421   65787

Upvotes: 1

Views: 78

Answers (3)

jhansen
jhansen

Reputation: 1136

It looks like this is what you are trying to do in your output:

df = pd.DataFrame(
    [[1456, 6744, 9876, 374, 65413, 1456],
     [654, 2314, 674654, 2156, 872, 6744],
     [875, 653, 36541, 345, 4963, 9876],
     [6875, 7401, 3654, 465, 3547, 374],
     [78654, 8662, 35, 6987, 6874, 65413],
     [658, 94512, 687, 489, 8756, 5854]],
    columns=list('ABCDEF'))

def fn(col):
    return col[:-2].values + col[1:-1].values

Apply the function as mentioned in previous answers:

>>> df.apply(fn)
        A   B   C   D   E   F
0   2110    9058    684530  2530    66285   8200
1   1529    2967    711195  2501    5835    16620
2   7750    8054    40195   810 8510    10250
3   85529   16063   3689    7452    10421   65787

Upvotes: 0

Suhas_Pote
Suhas_Pote

Reputation: 4580

Here is simple example

df

    A   B   C   D
0  10  11  12  13
1  20  21  22  23
2  30  31  32  33
3  40  41  42  43

# Assume your user defined function is 
def mul(x, y):
  return x * y

which will multiply the values

Let's say you want to multiply first column 'A' with 3

df['A'].apply(lambda x: mul(x,3))

0     30
1     60
2     90
3    120

Now, you want to apply mul function to all columns of dataframe and create new dataframe with results

df1 = df.applymap(lambda x: mul(x, 3))

df1

     A    B    C    D
0   30   33   36   39
1   60   63   66   69
2   90   93   96   99
3  120  123  126  129

Upvotes: 2

ggrelet
ggrelet

Reputation: 1101

pd.DataFrame object also has its own apply method.

From the example given in the documentation of the link above:

>>> df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B'])
>>> df
   A  B
0  4  9
1  4  9
2  4  9
>>> df.apply(np.sqrt)
     A    B
0  2.0  3.0
1  2.0  3.0
2  2.0  3.0

Conclusion: you should be able to apply your function to the whole dataframe.

Upvotes: 0

Related Questions