I’m trying to improve my skills in data science with some courses, and today I was taking a lesson about missing values and how to deal with them. During the lesson it showed this code:
# Calculate median plane ticket prices by Airline
airline_prices = planes.groupby("Airline")["Price"].median()
print(airline_prices)
# Convert to a dictionary
prices_dict = airline_prices.to_dict()
# Map the dictionary to missing values of Price by Airline
planes["Price"] = planes["Price"].fillna(planes["Airline"].map(prices_dict))
# Check for missing values
print(planes.isna().sum())
I’m having difficult to understand what the map function does in this case. I know that map function applies a function to every item of the iterable passed as second argument, but in this case is passed only the iterable argument (which is a dictionary in this case). Please tell me if I’m right or not: in that line of code for every row of the ‘Price’ column that has a na value the fillna function fill it with the median of each group of ‘Airline’. In this the map function simply associate every ‘Airline’ to the corresponding median? Why the map function works without a func passed to it?
LucÀ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.