Challenge - Movie Ratings¶
You’re given a 10x2 array of floats where each row represents a movie. The first column represents the movie’s rating and the second column represents the director’s rating.
Create a third column that represents the overall rating. The overall rating is equal to the movie rating if it exists, otherwise the director’s rating.
In [1]:
import numpy as np
generator = np.random.default_rng(123)
ratings = np.round(generator.uniform(low=0.0, high=10.0, size=(10, 2)))
ratings[[1,2,7,9], [0,0,0,0]] = np.nan
print(ratings)
# [[ 7. 1.]
# [nan 2.]
# [nan 8.]
# [ 9. 3.]
# [ 8. 9.]
# [ 5. 2.]
# [ 8. 2.]
# [nan 6.]
# [ 9. 2.]
# [nan 5.]]
[[ 7. 1.] [nan 2.] [nan 8.] [ 9. 3.] [ 8. 9.] [ 5. 2.] [ 8. 2.] [nan 6.] [ 9. 2.] [nan 5.]]
In [56]:
nan_cols = np.isnan(ratings[:, 0])
final_ratings = np.zeros(ratings.shape[0])
final_ratings[nan_cols] = ratings[:, 1][nan_cols]
final_ratings[~nan_cols] = ratings[:, 0][~nan_cols]
np.concatenate((ratings, np.array([final_ratings]).T), axis=-1)
Out[56]:
array([[ 7., 1., 7.], [nan, 2., 2.], [nan, 8., 8.], [ 9., 3., 9.], [ 8., 9., 8.], [ 5., 2., 5.], [ 8., 2., 8.], [nan, 6., 6.], [ 9., 2., 9.], [nan, 5., 5.]])
In [ ]: