Reputation: 475
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
Upvotes: 0
Views: 42
Reputation: 863541
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
Upvotes: 1