Apply a function to a dataframe and only return part of the dictionary in Python

344 views Asked by At

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.

2

There are 2 answers

3
Ricardo Erikson On BEST ANSWER

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.

1
esanchez01 On

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.