← Back to Pandas Course | Chapter 8: Merging & Joining | Lesson 6 of 7

merge_asof() का उपयोग

merge_asof exact match के बजाय nearest key पर join करता है, जो time series के लिए आदर्श है।

In this page:

  1. merge_asof()
Syntax
python
merged = pd.merge_asof(left, right, on="sorted_column")

merge_asof()

pd.merge_asof left table की हर row को right-hand की उस आखिरी row से match करता है जिसकी key उससे कम या बराबर हो। दोनों tables को key के अनुसार sorted होना चाहिए। backward, forward या nearest चुनने के लिए direction का, और groups match करने के लिए by का उपयोग करें।

Note: आम उपयोग है हर trade के साथ latest price quote जोड़ना।

उदाहरण: merge_asof()

python
import pandas as pd

trades = pd.DataFrame({"time": pd.to_datetime(["10:00:05", "10:00:15"]), "qty": [100, 200]})
quotes = pd.DataFrame({"time": pd.to_datetime(["10:00:00", "10:00:10", "10:00:20"]), "price": [50.0, 51.0, 52.0]})
print(pd.merge_asof(trades, quotes, on="time"))

# Output:
#                  time  qty  price
# 0 2026-09-20 10:00:05  100   50.0
# 1 2026-09-20 10:00:15  200   51.0
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. पहले key के अनुसार sort न करना
  2. इसे exact-match joins के लिए उपयोग करना
  3. stale matches के लिए tolerance भूल जाना
चैप्टर सारांश
  • nearest key पर match करता है
  • Inputs sorted होने चाहिए
  • direction search side चुनता है
  • by पहले groups match करता है
🔒

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.