0

The use case is the following: If in a Pandas Dataframe, several columns are greater than zero, I want to create a new column with value 1, if the same columns are negative, I wish to set -1, otherwise I wish to set 0.

Now, I want to extend the previous. Let's say I want to check for 4 columns the conditions, but I still wish to assign the corresponding value if three of them hold. An example below.

import pandas as pd
import numpy as np

df = pd.DataFrame(
    [
        [1, 2, 3, 4, 5],
        [-1, -2, -3, -4, -5],
        [1, 2, -1, -2, -3],
        [1, 2, 3, -1, -2]
    ]
    , columns=list('ABCDE'))


def f(df):
    dst = pd.Series(np.zeros(df.shape[0], dtype=int))
    dst[(df < 0).all(1)] = -1
    dst[(df > 0).all(1)] = 1
    return dst

columns = ['A', 'B', 'C', 'D']

df['dst'] = f(df[columns])

     

The code above would return the following DataFrame:

   A  B  C  D  E  dst
0  1  2  3  4  5    1
1 -1 -2 -3 -4 -5   -1
2  1  2 -1 -2 -3    0
3  1  2  3 -1 -2    0

What would be the expected behavior:

  1. For row 0, dst should be 1 as A to D hold the positive condition.
  2. For row 1, dst should be -1 as A to D hold the negative condition.
  3. For row 2, dst should be 0 as A to D do not meet any of the conditions.
  4. For row 3, dst should be 1 as A to C hold the positive condition, and only D does not hold.
Alessandroempire
  • 1,640
  • 4
  • 31
  • 54
  • 4
    Sample input and expected output would make it more clear. Can you add them to the question? – Ch3steR Nov 23 '20 at 19:52
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Nov 23 '20 at 20:16

1 Answers1

0

You could use the loc() method withing padas DataFrame object. Like this:

input_df.loc[conditions[0], dst_col] = choices[0]
input_df.loc[conditions[1], dst_col] = choices[1]

This will basically filter input_df by both conditions and assign the proper choice to that.

EnriqueBet
  • 1,482
  • 2
  • 15
  • 23