'unable to assign Null values to a Dataframe?

I was working on a dataset where I found some null values. It's basically a Book Recommendation System. I have a column Author with author names with some null values. I used selenium to extract the Author's name using the Title. I stored the name in a list.

Now, I wish to enter these authors' names into my data frame, at the indexes where the actual column author is having null values.

Can anyone help me with this issue?

Dataset Image

Selenium Code.

List b contains all the authors' names extracted using selenium. List nan contains indexes of the missing values of the Author column.

Code I tried to assign the values

Indexes of the missing values in book data frame Author column



Solution 1:[1]

Assuming the author list is same length as missing values, you could zip through both lists for assignment:

for x, y in zip(authors_nan_indices, authors_list):
    book["Author"][x] = y

But in Pandas, aim for vectorized operations over looping:

book.loc[pd.isnull(book["Author"]), "Author"] = authors_list

Or with numpy.where using equality since NaN != NaN

book["Author"] = np.where(
    book["Author"] == book["Author"],
    book["Author"], 
    np.array(authors_list)
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parfait