← Back to Pandas Course | Chapter 13: Performance & Best Practices | Lesson 2 of 6

pd.eval() का उपयोग

pd.eval और df.eval expressions को strings के रूप में evaluate करते हैं, जो बड़े arithmetic के लिए तेज़ और साफ़ हो सकता है।

In this page:

  1. pd.eval()
Syntax
python
df.eval("new_column = column1 + column2")
pd.eval("result = df.column1 + df.column2")

pd.eval()

df.eval("c = a + b") एक expression string से column बनाता है और बड़े data पर temporary arrays से बच सकता है। यह arithmetic, comparisons और @ से local variables को reference करने को support करता है। फायदे ज़्यादातर बड़े DataFrames पर दिखते हैं।

Note: छोटे DataFrames के लिए plain Python उतना ही तेज़ है।

उदाहरण: pd.eval()

python
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
df.eval("c = a + b * 2", inplace=True)
factor = 3
print(df.eval("a * @factor").tolist())
print(df)

# Output:
# [3, 6, 9]
#    a   b   c
# 0  1  10  21
# 1  2  20  42
# 2  3  30  63
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. छोटे data पर speed-ups की उम्मीद करना
  2. local variables के लिए @ भूल जाना
  3. expressions में unsupported functions का उपयोग करना
चैप्टर सारांश
  • eval string expressions evaluate करता है
  • नए columns assign करता है
  • @var locals को reference करता है
  • बड़े data पर सबसे अच्छा
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.