What does this line of code using pandas dataframes mean?

Question:

I understand what .get() means, it’s used retrieve values of keys in a dict variable.

However, I am trying to completely understand the following code to the granular level:

df['image_ID'].map(image_location.get)

Where df is a DataFrame. And image_location is the directory of each image in the DataFrame.

I think I understand what is happening here, but please correct me if I am wrong:

.get() is obtaining the path to each image, and then linking it to the image_ID, so they can be on the same row of the DataFrame.

I don’t understand though, what .map is doing? I’m not sure I understand this completely too.

Asked By: PKrange

||

Answers:

From the docs,

Used for substituting each value in a Series with another value, that
may be derived from a function, a dict or a Series.

Each value in the column will be replaced by the value returned by calling image_location.get(value)

This is equivalent to:

for i, value in enumerate(df['Image_ID']):
    df['Image_ID'][i] = image_location.get(value)
Answered By: tdelaney

It takes the image_id series and for each item in the series it calls the function identified with image_location.get (passing in the image id to the function) and returns that as a series

Answered By: LhasaDad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.