Home > Backend Development > Python Tutorial > How to Efficiently Calculate the Cartesian Product of DataFrames in Pandas?

How to Efficiently Calculate the Cartesian Product of DataFrames in Pandas?

Mary-Kate Olsen
Release: 2024-12-07 17:32:13
Original
470 people have browsed it

How to Efficiently Calculate the Cartesian Product of DataFrames in Pandas?

Cartesian Product in Pandas

When working with two or more dataframes in Pandas, there may be a need to obtain their cartesian product, which results in a new dataframe with all combinations of rows from the input dataframes.

Pandas >= 1.2

In recent versions of Pandas, the merge function can be utilized to perform this operation with the how='cross' argument. This approach is both concise and efficient:

import pandas as pd

df1 = pd.DataFrame({'col1':[1,2],'col2':[3,4]})
df2 = pd.DataFrame({'col3':[5,6]})

df_cartesian = df1.merge(df2, how='cross')
Copy after login

Pandas < 1.2

For earlier versions of Pandas, a slightly different technique is required. It involves creating a key column that is repeated for each row in both dataframes. Once this key column is added, merge can be used to perform the cartesian product:

import pandas as pd
from pandas import merge

df1 = pd.DataFrame({'key':[1,1], 'col1':[1,2],'col2':[3,4]})
df2 = pd.DataFrame({'key':[1,1], 'col3':[5,6]})

merge(df1, df2,on='key')[['col1', 'col2', 'col3']]
Copy after login

This approach is more involved but works effectively in older versions of Pandas.

The above is the detailed content of How to Efficiently Calculate the Cartesian Product of DataFrames in Pandas?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template