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

apply के साथ lambda

एक lambda एक tiny one-line function है, quick apply और map calls के लिए perfect।
Syntax
python
df["column"].apply(lambda x: expression)
df.apply(lambda row: expression, axis=1)

apply के साथ lambda

lambda x: expression inline एक unnamed function define करता है। apply के साथ combine होकर यह आपको एक पूरी def लिखे बिना custom logic से नए columns compute करने देता है। Multi-step logic के लिए एक named function ज़्यादा readable है।

Note: Row-wise lambdas नाम से columns access करते हैं, जैसे lambda r: r["a"] + r["b"]।

उदाहरण: lambda with apply

python
import pandas as pd

df = pd.DataFrame({"name": ["ann", "bob"], "score": [82, 47]})
df["name"] = df["name"].apply(lambda s: s.title())
df["result"] = df["score"].apply(lambda x: "pass" if x >= 50 else "fail")
print(df)

# Output:
#   name  score result
# 0  Ann     82   pass
# 1  Bob     47   fail
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. लंबी unreadable lambdas लिखना
  2. row values इस्तेमाल करते वक्त axis=1 भूल जाना
  3. जहाँ np.where simpler हो वहाँ एक lambda इस्तेमाल करना
चैप्टर सारांश
  • lambda एक inline function है
  • apply और map के साथ काम करता है
  • Rows के लिए axis=1 इस्तेमाल करें
  • Complex logic के लिए def इस्तेमाल करें
🔒

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.