← Back to Pandas Course | Chapter 6: Data Operations | Lesson 2 of 7

map() का उपयोग

map एक dictionary या एक function इस्तेमाल करके एक Series में हर value convert करता है।

In this page:

  1. map()
Syntax
python
df["column"].map({"old": "new"})
df["column"].map(function_name)

map()

Series.map एक dict, एक Series, या एक function accept करता है। एक dict के साथ, unmatched values NaN बन जाती हैं। यह "M" और "F" को पूरे words में बदलने जैसी categories recode करने के लिए perfect है।

Note: एक dict के साथ map अक्सर एक column recode करने का सबसे fast तरीका है।

उदाहरण: map()

python
import pandas as pd

s = pd.Series(["M", "F", "M", "X"])
print(s.map({"M": "Male", "F": "Female"}))
print(pd.Series([1, 2, 3]).map(lambda x: x * 10))

# Output:
# 0      Male
# 1    Female
# 2      Male
# 3       NaN
# dtype: object
# 0    10
# 1    20
# 2    30
# dtype: int64
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि unmatched keys unchanged रहेंगी
  2. पूरे DataFrame पर map इस्तेमाल करना
  3. यह भूल जाना कि यह एक नई Series return करता है
चैप्टर सारांश
  • map dict, Series, या function accept करता है
  • Unmatched values NaN बन जाती हैं
  • यह एक Series पर काम करता है
  • Recoding के लिए ideal
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.