Use polars .when() instead joins

I have 3 polars dataframes, one that contains 2 IDS, and the other ones contains an ID and a value. I would like to join the 3 dataframes if the ID of the main table exists on one of the other tables and bring a values from a desired column.

My current aproach its just rename each Table ID and then do a .join(how = 'left'), however, i think renaming and duplicate tables its not the correct way to approach this problem. (Due the extra code, and the wasted ram)

The first one contains 2 ID columns:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>data = {
"ID1" : [1,2,3],
"ID2" : [1,4,5]
}
df = pl.DataFrame(data)
</code>
<code>data = { "ID1" : [1,2,3], "ID2" : [1,4,5] } df = pl.DataFrame(data) </code>
data = {
    "ID1" : [1,2,3],
    "ID2" : [1,4,5]
}
df = pl.DataFrame(data)

The second and third are dataframes than contains an ID and a value:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>T1 = {
"ID" : [9,2,5],
"Values" : ["A","B","c"],
"Values II" : ["foo","boo","baz"]
}
T1 = pl.DataFrame(T1)
T2 = {
"ID" : [1,4,10],
"Values" : ["X","J","c"]
}
T2 = pl.DataFrame(T2)
</code>
<code>T1 = { "ID" : [9,2,5], "Values" : ["A","B","c"], "Values II" : ["foo","boo","baz"] } T1 = pl.DataFrame(T1) T2 = { "ID" : [1,4,10], "Values" : ["X","J","c"] } T2 = pl.DataFrame(T2) </code>
T1 = {
    "ID" : [9,2,5],
    "Values" : ["A","B","c"],
    "Values II" : ["foo","boo","baz"]
}
T1 = pl.DataFrame(T1)

T2 = {
    "ID" : [1,4,10],
    "Values" : ["X","J","c"]
}
T2 = pl.DataFrame(T2)

I can check if the ID exists on the other tables like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>(
df
.with_columns(
ID1_is_on_T1 = pl.col("ID1").is_in(T1.select(pl.col("ID"))),
ID2_is_on_T1 = pl.col("ID2").is_in(T1.select(pl.col("ID"))),
ID1_is_on_T2 = pl.col("ID1").is_in(T2.select(pl.col("ID"))),
ID2_is_on_T2 = pl.col("ID2").is_in(T2.select(pl.col("ID"))),
)
)
</code>
<code>( df .with_columns( ID1_is_on_T1 = pl.col("ID1").is_in(T1.select(pl.col("ID"))), ID2_is_on_T1 = pl.col("ID2").is_in(T1.select(pl.col("ID"))), ID1_is_on_T2 = pl.col("ID1").is_in(T2.select(pl.col("ID"))), ID2_is_on_T2 = pl.col("ID2").is_in(T2.select(pl.col("ID"))), ) ) </code>
(
    df
    .with_columns(
        ID1_is_on_T1 = pl.col("ID1").is_in(T1.select(pl.col("ID"))),
        ID2_is_on_T1 = pl.col("ID2").is_in(T1.select(pl.col("ID"))),
        ID1_is_on_T2 = pl.col("ID1").is_in(T2.select(pl.col("ID"))),
        ID2_is_on_T2 = pl.col("ID2").is_in(T2.select(pl.col("ID"))),
    )
)

And i’m looking to do somehting like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>(
df
.with_columns(
pl
.when(
pl.col("ID1").is_in(T1.select(pl.col("ID")))
)
.then(
T1.select(pl.col("Values"))
)
.otherwise(0)
)
)
</code>
<code>( df .with_columns( pl .when( pl.col("ID1").is_in(T1.select(pl.col("ID"))) ) .then( T1.select(pl.col("Values")) ) .otherwise(0) ) ) </code>
(
    df
    .with_columns(
        pl
        .when(
            pl.col("ID1").is_in(T1.select(pl.col("ID")))
        )
        .then(
            T1.select(pl.col("Values"))
        )
        .otherwise(0)
    )
)

ValueError: can only call .item() if the dataframe is of shape (1, 1), or if explicit row/col values are provided; frame has shape (3, 1)

Current .join() approach:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>T1_1 = (
T1
.rename(
{"ID": "ID1"}
)
)
T1_2 = (
T1
.rename(
{"ID": "ID2"}
)
)
Join_1 = df.join(T1_1,on = "ID1", how="left").rename({"Values" : "ID1_Values", "Values II" : "ID1_Values II"})
Join_2 = Join_1.join(T1_2, on = "ID2", how="left").rename({"Values" : "ID2_Values", "Values II" : "ID2_Values II"})
</code>
<code>T1_1 = ( T1 .rename( {"ID": "ID1"} ) ) T1_2 = ( T1 .rename( {"ID": "ID2"} ) ) Join_1 = df.join(T1_1,on = "ID1", how="left").rename({"Values" : "ID1_Values", "Values II" : "ID1_Values II"}) Join_2 = Join_1.join(T1_2, on = "ID2", how="left").rename({"Values" : "ID2_Values", "Values II" : "ID2_Values II"}) </code>
T1_1 = (
    T1
    .rename(
        {"ID": "ID1"}
    )
)

T1_2 = (
    T1
    .rename(
        {"ID": "ID2"}
    )
)

Join_1 = df.join(T1_1,on = "ID1", how="left").rename({"Values" : "ID1_Values", "Values II" : "ID1_Values II"})
Join_2 = Join_1.join(T1_2, on = "ID2", how="left").rename({"Values" : "ID2_Values", "Values II" : "ID2_Values II"})

On this approach its only considering the first table, i would need to do the same for the T2 too.

4

Two joins will be most efficient here. You can avoid excessive renaming by specifing left_on and right_on separately, as well as using a suffix to clarify the duplicate names.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>>>> (df.join(T1, left_on="ID1", right_on="ID", how="left")
... .join(T2, left_on="ID2", right_on="ID", how="left", suffix="_T2"))
shape: (3, 5)
┌─────┬─────┬────────┬───────────┬───────────┐
│ ID1 ┆ ID2 ┆ Values ┆ Values II ┆ Values_T2 │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str ┆ str ┆ str │
╞═════╪═════╪════════╪═══════════╪═══════════╡
11nullnull ┆ X │
24 ┆ B ┆ boo ┆ J │
35nullnullnull
└─────┴─────┴────────┴───────────┴───────────┘
</code>
<code>>>> (df.join(T1, left_on="ID1", right_on="ID", how="left") ... .join(T2, left_on="ID2", right_on="ID", how="left", suffix="_T2")) shape: (3, 5) ┌─────┬─────┬────────┬───────────┬───────────┐ │ ID1 ┆ ID2 ┆ Values ┆ Values II ┆ Values_T2 │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ str ┆ str ┆ str │ ╞═════╪═════╪════════╪═══════════╪═══════════╡ │ 1 ┆ 1 ┆ null ┆ null ┆ X │ │ 2 ┆ 4 ┆ B ┆ boo ┆ J │ │ 3 ┆ 5 ┆ null ┆ null ┆ null │ └─────┴─────┴────────┴───────────┴───────────┘ </code>
>>> (df.join(T1, left_on="ID1", right_on="ID", how="left")
...    .join(T2, left_on="ID2", right_on="ID", how="left", suffix="_T2"))
shape: (3, 5)
┌─────┬─────┬────────┬───────────┬───────────┐
│ ID1 ┆ ID2 ┆ Values ┆ Values II ┆ Values_T2 │
│ --- ┆ --- ┆ ---    ┆ ---       ┆ ---       │
│ i64 ┆ i64 ┆ str    ┆ str       ┆ str       │
╞═════╪═════╪════════╪═══════════╪═══════════╡
│ 1   ┆ 1   ┆ null   ┆ null      ┆ X         │
│ 2   ┆ 4   ┆ B      ┆ boo       ┆ J         │
│ 3   ┆ 5   ┆ null   ┆ null      ┆ null      │
└─────┴─────┴────────┴───────────┴───────────┘

If instead of storing T1 and T2 as their own variables, you put them in a dictionary, you could make a function that does it all for you.

Setup variables like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df = pl.DataFrame({
"ID1" : [1,2,3],
"ID2" : [1,4,5]
})
T={}
T["T1"]=pl.DataFrame({
"ID" : [9,2,5],
"Values" : ["A","B","c"],
"Values II" : ["foo","boo","baz"]
}
)
T["T2"]=pl.DataFrame({
"ID" : [1,4,10],
"Values" : ["X","J","c"]
})
</code>
<code>df = pl.DataFrame({ "ID1" : [1,2,3], "ID2" : [1,4,5] }) T={} T["T1"]=pl.DataFrame({ "ID" : [9,2,5], "Values" : ["A","B","c"], "Values II" : ["foo","boo","baz"] } ) T["T2"]=pl.DataFrame({ "ID" : [1,4,10], "Values" : ["X","J","c"] }) </code>
df = pl.DataFrame({
    "ID1" : [1,2,3],
    "ID2" : [1,4,5]
})

T={}
T["T1"]=pl.DataFrame({
    "ID" : [9,2,5],
    "Values" : ["A","B","c"],
    "Values II" : ["foo","boo","baz"]
}
)
T["T2"]=pl.DataFrame({
    "ID" : [1,4,10],
    "Values" : ["X","J","c"]
})

And then the function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def big_join(df:pl.DataFrame, T:dict[str, pl.DataFrame])->pl.DataFrame:
joined=df
for id_col in df.columns:
for tbl_name, subdf in T.items():
joined=joined.join(
subdf.select("ID", pl.exclude("ID").name.prefix(f"{id_col}_{tbl_name}_")),
left_on=id_col, right_on='ID', how='left'
)
return joined
</code>
<code>def big_join(df:pl.DataFrame, T:dict[str, pl.DataFrame])->pl.DataFrame: joined=df for id_col in df.columns: for tbl_name, subdf in T.items(): joined=joined.join( subdf.select("ID", pl.exclude("ID").name.prefix(f"{id_col}_{tbl_name}_")), left_on=id_col, right_on='ID', how='left' ) return joined </code>
def big_join(df:pl.DataFrame, T:dict[str, pl.DataFrame])->pl.DataFrame:
    joined=df
    for id_col in df.columns:
        for tbl_name, subdf in T.items():
            joined=joined.join(
                subdf.select("ID", pl.exclude("ID").name.prefix(f"{id_col}_{tbl_name}_")),
                left_on=id_col, right_on='ID', how='left'
                )
    return joined

When you want to join them all up, just do

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>big_join(df, T)
shape: (3, 8)
┌─────┬─────┬──────────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┐
│ ID1 ┆ ID2 ┆ ID1_T1_Value ┆ ID1_T1_Value ┆ ID1_T2_Value ┆ ID2_T1_Valu ┆ ID2_T1_Valu ┆ ID2_T2_Valu │
│ --- ┆ --- ┆ s ┆ s II ┆ s ┆ es ┆ es II ┆ es │
│ i64 ┆ i64 ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ ┆ ┆ str ┆ str ┆ str ┆ str ┆ str ┆ str │
╞═════╪═════╪══════════════╪══════════════╪══════════════╪═════════════╪═════════════╪═════════════╡
11nullnull ┆ X ┆ nullnull ┆ X │
24 ┆ B ┆ boo ┆ nullnullnull ┆ J │
35nullnullnull ┆ c ┆ baz ┆ null
└─────┴─────┴──────────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┘
</code>
<code>big_join(df, T) shape: (3, 8) ┌─────┬─────┬──────────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┐ │ ID1 ┆ ID2 ┆ ID1_T1_Value ┆ ID1_T1_Value ┆ ID1_T2_Value ┆ ID2_T1_Valu ┆ ID2_T1_Valu ┆ ID2_T2_Valu │ │ --- ┆ --- ┆ s ┆ s II ┆ s ┆ es ┆ es II ┆ es │ │ i64 ┆ i64 ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ ┆ ┆ str ┆ str ┆ str ┆ str ┆ str ┆ str │ ╞═════╪═════╪══════════════╪══════════════╪══════════════╪═════════════╪═════════════╪═════════════╡ │ 1 ┆ 1 ┆ null ┆ null ┆ X ┆ null ┆ null ┆ X │ │ 2 ┆ 4 ┆ B ┆ boo ┆ null ┆ null ┆ null ┆ J │ │ 3 ┆ 5 ┆ null ┆ null ┆ null ┆ c ┆ baz ┆ null │ └─────┴─────┴──────────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┘ </code>
big_join(df, T)
shape: (3, 8)
┌─────┬─────┬──────────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┐
│ ID1 ┆ ID2 ┆ ID1_T1_Value ┆ ID1_T1_Value ┆ ID1_T2_Value ┆ ID2_T1_Valu ┆ ID2_T1_Valu ┆ ID2_T2_Valu │
│ --- ┆ --- ┆ s            ┆ s II         ┆ s            ┆ es          ┆ es II       ┆ es          │
│ i64 ┆ i64 ┆ ---          ┆ ---          ┆ ---          ┆ ---         ┆ ---         ┆ ---         │
│     ┆     ┆ str          ┆ str          ┆ str          ┆ str         ┆ str         ┆ str         │
╞═════╪═════╪══════════════╪══════════════╪══════════════╪═════════════╪═════════════╪═════════════╡
│ 1   ┆ 1   ┆ null         ┆ null         ┆ X            ┆ null        ┆ null        ┆ X           │
│ 2   ┆ 4   ┆ B            ┆ boo          ┆ null         ┆ null        ┆ null        ┆ J           │
│ 3   ┆ 5   ┆ null         ┆ null         ┆ null         ┆ c           ┆ baz         ┆ null        │
└─────┴─────┴──────────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┘

This is extensible to any number of T tables so long as they’re in the dictionary. Even if you wanted to maintain T1 and T2 as their own variables you could do big_join(df, {"T1":T1, "T2":T2}) instead.

Polars doesn’t copy unless it needs to so you don’t need to worry about wasted ram. For example, if you do df1=df2=df3=df it doesn’t copy df 3 times, it just has a pointer in each of the “copies” pointing back to df. The same is true for columns so if you left join df to T1 then it isn’t copying df to the new join table, it just points to the columns. I’m not a good authority on when exactly copies are made/needed but it is far fewer than other bear based DataFrame libraries.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật