I have a data frame where I am trying to create a new column when only a specific condition is met.
For example I have this line of code that I want to change to display a different output.
data['new_filtered'] = np.where(((data.movie == data.genre) & (data.length > movie_average)),1,0)
Instead of the return to be 1 and 0 for True and False, I want it to display data.movie
but 5 index positions.
For Example, if this condition is met at row 5 I want the return to be the movie at row 10. I have done this in the past with just a for loop and appending that return value to a list and added that to the dataframe. I am just curious to see if this is possible with np.where().
CodePudding user response:
Try something like:
mask = ((data.movie == data.genre) & (data.length > movie_average))
data['new_filtered'] = np.where(mask, data.movie.shift(-5), 0)
# ^^^^^^^^^^^^^^^^^^^^