शर्त या नज़दीकी मिलान से DataFrames जोड़ना

Polars के साथ Data Transformation

Liam Brannigan

Data Scientist & Polars Contributor

शर्त पर जॉइन करना

एकल टेबल जिसमें वर्ल्ड कप विजेताओं की सूची है

Polars के साथ Data Transformation

शर्त पर जॉइन करना

एक टेबल में वर्ल्ड कप विजेता और दूसरी में सिल्वर/गोल्ड के लिए न्यूनतम जीतें.

Polars के साथ Data Transformation

शर्त पर जॉइन करना

वर्ल्ड कप विजेताओं की टेबल, स्तरों की टेबल, और जॉइन की हुई टेबल जिसमें हर विजेता का स्तर दिखा है

Polars के साथ Data Transformation

शर्त पर जॉइन करना

वर्ल्ड कप विजेताओं की टेबल, स्तरों की टेबल, और जॉइन की हुई टेबल जिसमें ब्राज़ील की दो पंक्तियाँ हाइलाइट हैं.

Polars के साथ Data Transformation

पसंद वाले ऐप यूज़र्स

restaurants = pl.read_csv("restuarants.csv")
shape: (4, 5)
| business        | location    | review | price | type       |
| ---             | ---         | ---    | ---   | ---        |
| str             | str         | f64    | i64   | str        |
|-----------------|-------------|--------|-------|------------|
| 7burgers        | Wakey Wakey | 4.2    | 15    | restaurant |
| Costa Coffee    | City Point  | 4.5    | 8     | café       |
| Costa Coffee    | Waterloo    | 4.1    | 8     | café       |
| The Queens Head | Denman St.  | 4.7    | 25    | bar        |
Polars के साथ Data Transformation

Users टेबल

users = pl.read_csv("app_users.csv")
shape: (3, 3)
| user_name | budget | type       |
| ---       | ---    | ---        |
| str       | i64    | str        |
|-----------|--------|------------|
| Bob       | 12     | café       |
| Alice     | 15     | restaurant |
| Charlie   | 22     | bar        |
Polars के साथ Data Transformation

शर्त पर जॉइन करना

restaurants.join_where(


)
Polars के साथ Data Transformation

शर्त पर जॉइन करना

restaurants.join_where(
    users,

)
Polars के साथ Data Transformation

शर्त पर जॉइन करना

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right")
)
Polars के साथ Data Transformation

शर्त पर जॉइन करना

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right")
)
shape: (4, 8)
| business        | type       | ... | user_name | budget |
| ---             | ---        | ... | ---       | ---    |
| str             | str        | ... | str       | i64    |
|-----------------|------------|-----|-----------|--------|
| 7burgers        | restaurant | ... | Alice     | 15     |
| Costa Coffee    | café       | ... | Bob       | 12     |
| Costa Coffee    | café       | ... | Bob       | 12     |
| The Queens Head | bar        | ... | Charlie   | 22     |
Polars के साथ Data Transformation

एकाधिक शर्तों पर जॉइन करना

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right"),
    pl.col("price") <= pl.col("budget")
)
shape: (3, 8)
| user_name | budget | business     | price | type       | ... |
| ---       | ---    | ---          | ---   | ---        | ... |
| str       | i64    | str          | i64   | str        | ... |
|-----------|--------|--------------|-------|------------|-----|
| Alice     | 15     | 7burgers     | 15    | restaurant | ... |
| Bob       | 12     | Costa Coffee | 8     | café       | ... |
| Bob       | 12     | Costa Coffee | 8     | café       | ... |
Polars के साथ Data Transformation

सबसे हाल की वैल्यू पर जॉइन करना

shape: (4, 3)
| business | date        | rating |
| ---      | ---         | ---    |
| str      | date        | i64    |
|----------|-------------|--------|
| 7burgers | 2025-03-15  | 5      |
| 7burgers | 2025-06-20  | 3      |
| 7burgers | 2025-09-10  | 4      |
| 7burgers | 2025-11-25  | 5      |

$$

  • Task: प्रत्येक review को सबसे प्रासंगिक inspection से मैच करें
shape: (3, 4)
| user   | business | date       | score |
| ---    | ---      | ---        | ---   |
| str    | str      | date       | f64   |
|--------|----------|------------|-------|
| Charlie | 7burgers | 2025-05-01 | 3.8   |
| Bob     | 7burgers | 2025-08-01 | 4.2   |
| Alice   | 7burgers | 2025-12-01 | 4.5   |
Polars के साथ Data Transformation

सबसे हाल की वैल्यू पर जॉइन करना

reviews.sort("date").join_asof(



)

$$

$$

$$

$$

  • join_asof - किसी तारीख तक का जॉइन
Polars के साथ Data Transformation

सबसे हाल की वैल्यू पर जॉइन करना

reviews.sort("date").join_asof(
    inspections.sort("date"),


)
Polars के साथ Data Transformation

सबसे हाल की वैल्यू पर जॉइन करना

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",

)
Polars के साथ Data Transformation

सबसे हाल की वैल्यू पर जॉइन करना

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",
    strategy="backward"
)
| user    | date       | score | ... | rating |
| ---     | ---        | ---   | ... | ---    |
| str     | date       | f64   | ... | i64    |
|---------|------------|-------|-----|--------|
| Charlie | 2025-05-01 | 3.8   | ... | 5      |
| Bob     | 2025-08-01 | 4.2   | ... | 3      |
| Alice   | 2025-12-01 | 4.5   | ... | 5      |
Polars के साथ Data Transformation

सबसे नज़दीकी वैल्यू पर जॉइन करना

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",
    strategy="nearest"
)
| user    | date       | score | ... | rating |
| ---     | ---        | ---   | ... | ---    |
| str     | date       | f64   | ... | i64    |
|---------|------------|-------|-----|--------|
| Charlie | 2025-05-01 | 3.8   | ... | 5      |
| Bob     | 2025-08-01 | 4.2   | ... | 4      |
| Alice   | 2025-12-01 | 4.5   | ... | 5      |
Polars के साथ Data Transformation

रणनीति चुनना

$$

दो टाइम-सीरीज़ टेबल वाला आरेख, प्रत्येक में दो पंक्तियाँ.

Polars के साथ Data Transformation

रणनीति चुनना - backward

$$

दो टाइम-सीरीज़ टेबल वाला आरेख, बाएँ की दोनों पंक्तियाँ दाएँ की पहली पंक्ति से जुड़ी हैं.

Polars के साथ Data Transformation

रणनीति चुनना - nearest

$$

दो टाइम-सीरीज़ टेबल वाला आरेख, बाएँ की हर पंक्ति दाएँ की संगत पंक्ति से जुड़ी है.

Polars के साथ Data Transformation

रणनीति चुनना - forward

$$

दो टाइम-सीरीज़ टेबल वाला आरेख, बाएँ की दोनों पंक्तियाँ दाएँ की दूसरी पंक्ति से जुड़ी हैं.

Polars के साथ Data Transformation

ग्रुप्स के भीतर मिलान करना

reviews.sort("date").join_asof(
    restaurants.sort("date"),
    on="date",

by=["business", "location"]
)
| user    | business        | location    | date       | score | ... | rating |
| ---     | ---             | ---         | ---        | ---   | ... | ---    |
| str     | str             | str         | date       | f64   | ... | i64    |
|---------|-----------------|-------------|------------|-------|-----|--------|
| Charlie | 7burgers        | Wakey Wakey | 2025-05-01 | 3.8   | ... | null   |
| Bob     | Costa Coffee    | City Point  | 2025-05-01 | 4.0   | ... | 5      |
| Bob     | 7burgers        | Wakey Wakey | 2025-08-01 | 4.2   | ... | null   |
| Alice   | 7burgers        | Wakey Wakey | 2025-12-01 | 4.5   | ... | 4      |
| Alice   | The Queens Head | Denman St.  | 2025-12-01 | 4.7   | ... | 5      |
Polars के साथ Data Transformation

अभ्यास करते हैं!

Polars के साथ Data Transformation

Preparing Video For Download...