Apply function row wise on pandas data frame on columns with numerical values

pdubois

I have the following data frame:

import pandas as pd
df = pd.DataFrame({'AAA' : ['w','x','y','z'], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]})

Which looks like this:

In [32]: df
Out[32]:
  AAA  BBB  CCC
0   w   10  100
1   x   20   50
2   y   30  -30
3   z   40  -50

What I want to do is to perform function operation on every row for every column except those with non-numerical value (in this case AAA). In the real case the non-numerical case is always on first column, and the rest (could be greater than 2 columns) are always numerical.

The final desired output is:

  AAA  BBB  CCC  Score
0   w   10  100  110
1   x   20   50   70
2   y   30  -30    0
3   z   40  -50  -10

I tried this but failed:

import numpy as np
df["Score"] = df.apply(np.sum, axis=1)

What's the right way to do it?

Update2:

This is the code that give SettingWithCopyWarning. Please fresh start the ipython for testing.

import pandas as pd
import numpy as np 
def cvscore(fclist):
    sd = np.std(fclist)
    mean = np.mean(fclist)
    cv = sd/mean
    return cv

def calc_cvscore_on_df(df):
    df["CV"] = df.iloc[:,1:].apply(cvscore, axis=1)
    return df

df3 = pd.DataFrame(np.random.randn(1000, 3), columns=['a', 'b', 'c'])
calc_cvscore_on_df(df3[["a","b"]])
unutbu

To select everything but the first column, you could use df.iloc[:, 1:]:

In [371]: df['Score'] = df.iloc[:, 1:].sum(axis=1)

In [372]: df
Out[372]: 
  AAA  BBB  CCC  Score
0   w   10  100    110
1   x   20   50     70
2   y   30  -30      0
3   z   40  -50    -10

To apply an arbitrary function, func, to each row:

df.iloc[:, 1:].apply(func, axis=1)

For example,

import numpy as np
import pandas as pd

def cvscore(fclist):
    sd = np.std(fclist)
    mean = np.mean(fclist)
    cv = sd/mean
    return cv

df = pd.DataFrame({'AAA' : ['w','x','y','z'], 'BBB' : [10,20,30,40],
                   'CCC' : [100,50,-30,-50]})

df['Score'] = df.iloc[:, 1:].apply(cvscore, axis=1)
print(df)

yields

  AAA  BBB  CCC     Score
0   w   10  100  1.211386
1   x   20   50  0.868377
2   y   30  -30       NaN
3   z   40  -50 -5.809058

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

From Dev

Apply custom function to two columns for every row in data frame in R

From Dev

How to apply custom function to pandas data frame for each row

From Dev

Apply FUN row-wise on data frame with integer and character variables

From Dev

Row-wise sort then concatenate across specific columns of data frame

From Dev

pandas - apply replace function with condition row-wise

From Dev

Calculate center of mass row-wise in a pandas data frame

From Dev

Apply a function row-wise to a data.table

From Dev

R Apply function on data frame columns

From Dev

how to apply duplicated function to the columns of a data frame

From Dev

How to apply scipy function on Pandas data frame

From Dev

Group by and apply custom function in pandas data frame

From Dev

R: Apply function on data frame A dependent on values of data frame B

From Dev

How to use pandas apply function on all columns of some rows of data frame

From Dev

How to use pandas apply function on all columns of some rows of data frame

From Dev

Pandas Data Frame function application to a specific row

From Dev

Apply if-else function row-wise

From Dev

Dividing columns of a data frame group-wise?

From Dev

Calculate ratio row wise in a data frame

From Dev

How to apply functions with multiple arguments on Pandas selected columns data frame

From Dev

Apply function in data frame

From Dev

How do I build a data frame row wise not column wise?

From Dev

Row-wise assignment of values to multiple columns

From Dev

R apply user define function on data frame columns

From Dev

R apply user define function on data frame columns

From Dev

Using dplyr to apply a function of several columns of an R data frame

From Dev

Split data.frame by variable and apply function referring to concrete row

From Dev

Perform row wise operation on two pandas columns

From Dev

Using row-wise column indices in a vector to extract values from data frame

Related Related

  1. 1

    pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

  2. 2

    Apply custom function to two columns for every row in data frame in R

  3. 3

    How to apply custom function to pandas data frame for each row

  4. 4

    Apply FUN row-wise on data frame with integer and character variables

  5. 5

    Row-wise sort then concatenate across specific columns of data frame

  6. 6

    pandas - apply replace function with condition row-wise

  7. 7

    Calculate center of mass row-wise in a pandas data frame

  8. 8

    Apply a function row-wise to a data.table

  9. 9

    R Apply function on data frame columns

  10. 10

    how to apply duplicated function to the columns of a data frame

  11. 11

    How to apply scipy function on Pandas data frame

  12. 12

    Group by and apply custom function in pandas data frame

  13. 13

    R: Apply function on data frame A dependent on values of data frame B

  14. 14

    How to use pandas apply function on all columns of some rows of data frame

  15. 15

    How to use pandas apply function on all columns of some rows of data frame

  16. 16

    Pandas Data Frame function application to a specific row

  17. 17

    Apply if-else function row-wise

  18. 18

    Dividing columns of a data frame group-wise?

  19. 19

    Calculate ratio row wise in a data frame

  20. 20

    How to apply functions with multiple arguments on Pandas selected columns data frame

  21. 21

    Apply function in data frame

  22. 22

    How do I build a data frame row wise not column wise?

  23. 23

    Row-wise assignment of values to multiple columns

  24. 24

    R apply user define function on data frame columns

  25. 25

    R apply user define function on data frame columns

  26. 26

    Using dplyr to apply a function of several columns of an R data frame

  27. 27

    Split data.frame by variable and apply function referring to concrete row

  28. 28

    Perform row wise operation on two pandas columns

  29. 29

    Using row-wise column indices in a vector to extract values from data frame

HotTag

Archive