How To Use map() in Python

preview_player
Показать описание
map() is a Python built in that lets you apply or "map" a function to each element in a list or iterable object. map() is one of the most useful Python built in functions for data analytics, since you will often be working with large sequence data types that need to be transformed in various ways.

Code used in the video:

# Map lets you apply a function to each element of a list

power_level_list = [1000, 4000, 16000, 150, 9001, 1500]

def greater_than_9000(x):
if x > 9000:
return True
return False

mapped = map(greater_than_9000, power_level_list)

print(mapped)

# Print each mapping

for m in mapped:
print(m)

# Extract mapping into new list

mapped_list = [*map(greater_than_9000, power_level_list)]

mapped_list

# Use map with a lambda function

[*map(lambda x: x > 9000, power_level_list)]

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .

Videos on Pandas map() operations:

How To Use apply() In Pandas (Python)

How To Use applymap() In Pandas (Python)
Рекомендации по теме
Комментарии
Автор

you are my favorite python teacher, your tutorials are seriously great and I love your leetcode solving videos as well. thanks for everything datadaft

charlinamaxwell
Автор

hi, thanks for the video. You help people grow and archive their dreams.

i_am_a_real_cat
Автор

Great video! Learned a lot from this. Also love the dbz reference.

williamhartfield
Автор

nice explanation as always. learned something new today.

anythingcommon
Автор

Please could you share the video for map on series object as it was not mentioned in the description as stated in the mentioned video

adilmajeed
Автор

thanks! with regards to numpy arrays, when I try to use map(func, array) I get an error:

File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable

domillima
Автор

which IDLE you use in this video? is this Jupyter?

Thanveerkamal