Last active
September 13, 2021 20:11
-
-
Save GuiMarthe/76d07ff5c202961d948cc599871ac1e3 to your computer and use it in GitHub Desktop.
A quick pandas function to return the cross product of two data frames, kind of emulating tidyr::crossing function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from functools import reduce | |
def crossing(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame: | |
""" | |
Returns a data frame with the cross product (every pair possible) of rows | |
between df1 and df2 | |
""" | |
df1["join_key"] = 0 | |
df2["join_key"] = 0 | |
rdf = df1.merge(df2, how="outer", on="join_key").drop(columns=["join_key"]) | |
df1.drop(columns=["join_key"], inplace=True) | |
df2.drop(columns=["join_key"], inplace=True) | |
return rdf | |
def crossing_many(*args): | |
""" | |
Computes the cross product of all data frames passed on to it. | |
""" | |
return reduce(crossing, args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment