Calculate new column in pandas using conditional on other columns

idt_tt

I have a dataframe which looks like this

pd.DataFrame({'A': [5, 2, -3],
   ...:       'B': [9, -1, 7],
   ...:       'C': [-5, 2, -6]})
Out[28]: 
   A  B  C
0  5  9 -5
1  2 -1  2
2 -3  7 -6

I would like to do the following

x > 0 -> (1+x)

x < 0 -> (1-x)^-1 (to the power of -1)

and then sum up all the columns.

resulting dataframe should look like this

   A  B  C   D        Logic
0  5  9 -5  16.1667   (1+5)+(1+9)+((1-(-5))^-1)
1  2 -1  2  6.5       (1+2)+((1-(-1))^-1)+(1+2)
2 -3  7 -6  8.3929    ((1-(-3)^-1)+(1+7)+((1-(-6))^-1)
Quang Hoang

Here you go with np.where:

df['D'] = np.where(df>0, 1+df, 1/(1-df)).sum(1)

Output:

   A  B  C          D
0  5  9 -5  16.166667
1  2 -1  2   6.500000
2 -3  7 -6   8.392857

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calculate new column as the mean of other columns pandas

New column in pandas DataFrame conditional on value of other columns

Pandas add a new column based on conditional logic of many other columns

Creating a new column on conditional of two other columns pandas

Pandas calculate a new column from multiple other columns and subset of rows

calculate column conditional on other columns r

How to calculate a new column using individual values of other columns in a formula?

create new column based on conditional of other columns

Conditional new column with other columns value

Column in pandas conditional on average of other columns

Python: Assign value to a new column in Pandas as list using other columns

Pandas: Create New Column using Values in Subgroup of Other Columns

how to create a new column conditional on other column by using existing columns in python

Conditional Styling in Pandas using other columns

Use lambda with pandas to calculate a new column conditional on existing column

Conditional split on creating new columns using pandas

How to calculate a new column based on other columns using a lookup approach in R?

How to sum values in a column using conditional statements of other columns in a pandas dataframe?

Creating a new column conditional on the character values of two other columns

Adding a new column to a matrix conditional on the values in the other columns

How to create new column conditional on existing columns in pandas dataframe using for loop

Pandas: How to sum columns based on conditional of other column values?

replacing the value of one column conditional on two other columns in pandas

Using Conditional Statement to Evaluate Column, Calculate, and Create New Colum in Dataframe

pandas creating a new column based on other other columns

join column names in a new pandas columns conditional on value

Pandas column content to new columns, with other original columns

Grouping a column based on values on other columns to create new columns in pandas

Pandas: Alter a column by condition using other columns