0

I have an example data as:

datetime    column1.   column2 
2020-01-01.   5.       [0,0,0,1]
2020-01-02.   4.       [0,0,0,0]
2020-01-03.   10.      [1,1,1,0]
2020-01-04.   2.       [1,1,1,1]

I want a new column called action which assumes: 1 if column1 values are below 3 and above 5 otherwise the df.column2.any(axis=1) values.

The example output should look like this:

datetime    column1.   column2    action
2020-01-01.   5.       [0,0,0,1].  1
2020-01-02.   2.       [0,0,0,0].  1
2020-01-03.   10.      [1,1,1,0].  1
2020-01-04.   4.       [0,0,0,0]   0
StackUser
  • 425
  • 1
  • 3
  • 12
  • 1
    I'm not sure how a value can be below 3 AND above 5, but you should try [this](https://stackoverflow.com/questions/48369929/creating-a-new-column-based-on-the-values-of-other-columns) – Amri Rasyidi Apr 22 '21 at 09:48

1 Answers1

1

Use numpy.where Series.between with any:

df['action'] = np.where(df.column1.between(3,5), df.column2.apply(any), 1)
print (df)
     datetime  column1       column2  action
0  2020-01-01        5  [0, 0, 0, 1]       1
1  2020-01-02        2  [0, 0, 0, 0]       1
2  2020-01-03       10  [1, 1, 1, 0]       1
3  2020-01-04        4  [0, 0, 0, 0]       0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252