Home > Net >  Iterate dataframe and assign value to each row- I get the same value while I want different ones
Iterate dataframe and assign value to each row- I get the same value while I want different ones

Time:03-22

I'm using the library isbntools to assign book titles to isbns. From a dataframe that has isbns, I want to create a column named title and assign the title to the corresponding isbn. Problem is I get the same title.

Example dataframe:

isbn

01234567

Desidred output

isbn, title 01234567, Curious George

Code:

from isbntools.app import *

for i in range(len(df_all['isbn'])):
    for isbnz in df_all['isbn']:
        meta_dict = meta(isbnz, service='goob')
        title = meta_dict['Title']
        df_all.iloc[i, df['Title'][i]]

I tried with iloc but it seems it didn't work

CodePudding user response:

Use:

# isbnlib is already installed as a dependency of isbntools
import isbnlib

def get_title(isbn):
    try:
        return isbnlib.meta(isbn)['Title']
    except isbnlib.NotValidISBNError:
        return None

df['Title'] = df['isbn'].astype(str).map(get_title)
>>> df
            isbn                  Title
0       01234567                   None
1  9780007525546  The Lord Of The Rings
  • Related