2

So I am trying to find out whether the previous number in a numpy array in a dataframe is greater than a number. So here is the code:

import pandas as pd
import numpy as np
arr_1 = np.array([7, 1, 6, 9, 2, 4])
arr_2 = np.array([5, 8, 9, 10, 2, 3])
arr_3 = np.array([1, 9, 3, 4, 5, 1])

dict_of_arrs = {
    'arr' : [arr_1, arr_2, arr_3]
}
df = pd.DataFrame(dict_of_arrs)
df

Which has an output of:

     arr
0   [7, 1, 6, 9, 2, 4]
1   [5, 8, 9, 10, 2, 3]
2   [1, 9, 3, 4, 5, 1]

So my questions is what code would be able to find out, if the secound number in each array for each row is greater than the first, and if the third is greater than the secound and so on for each number in each array. Where the code has an output that is True if the number is greater or False if the number is not in each array. Thank you.

0

1 Answer 1

3

I will do

df.arr.apply(lambda x : np.diff(x)>0)
0    [False, True, True, False, True]
1     [True, True, True, False, True]
2    [True, False, True, True, False]
Name: arr, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.