简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM