1

I have a df with an "isbn13" column. I also have a function called "isbnlib.meta". This function is from the library isbnlib. I would like to run the function on each row of the "isbn13" column. I'm using the apply function to do that.

df['publisher'] = df['isbn13'].apply(isbnlib.meta)

The issue is that the results for each isbn13 is a dictionary with various points such as Title, Author, Publisher, etc. I'm only looking for the "Publisher" result in the dictionary to be written out in my dataframe.

How do I only return the "Publisher" result in the dataframe from the dictionary results of the function?

Thank you in advance.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
CIHAnalytics
  • 143
  • 1
  • 10

2 Answers2

1

I suppose your isbnlib.meta() returns a dictionary based on the value in your isbn13 column. If so, you can use a lambda function in the same apply:

df['publisher'] = df['isbn13'].apply(lambda x: isbnlib.meta(x).get('Publisher', None))

In this case, if your dict doesn't have a Publisher key, it will return the default value None.

Ricardo Erikson
  • 435
  • 1
  • 4
  • 8
  • Hi Ricardo - Thanks for your response. I'm getting a key error "Publisher". I know that "Publisher" exists in the dictionary that's returned from the function because when I pass a single isbn13 as a string it returns with that dictionary option. – CIHAnalytics Oct 21 '20 at 01:17
  • It seems that some of your books don't have the `Publisher` key. You can use the `.get` method of your `dict` object to return a default value if the `Publisher` key doesn't exist. – Ricardo Erikson Oct 21 '20 at 01:24
  • This is brilliant and much cleaner than what I was starting to do. Thanks so much! – CIHAnalytics Oct 21 '20 at 01:29
0

I am unfamiliar with the isbnlib library but assuming that isbnlib.meta takes in a string and returns a dictionary, you can do:

df['publisher'] = df['isbn13'].apply(lambda x: isbnlib.meta(x)['Publisher'])

Using a lambda function inside .apply() can be very useful for simple tasks like this one.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thanks so much for responding. This didn't seem to work, but I'm certain I didn't explain this well enough. The input for this function is isbn13, which is a code for book identification. When you send a single isbn13 through the function it returns the meta data. The one I want to isolate is "Publisher". If I do this: isbn = '9780136858256' book = isbnlib.meta(isbn) publisher_isbnlib = book['Publisher'] publisher_isbnlib I get back 'Pearson' which is what I want. So I'm looking to do this function for each row of isbn13 from the dataframe. – CIHAnalytics Oct 21 '20 at 01:00