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

How to Calculate the Cartesian Product of DataFrames in Pandas?

DDD
Release: 2024-12-29 06:32:14
Original
448 people have browsed it

How to Calculate the Cartesian Product of DataFrames in Pandas?

How to Obtain a Cartesian Product in Pandas

In Pandas, a DataFrame is a tabular data structure. Performing operations on multiple DataFrames is often necessary for data analysis. One such operation is the Cartesian product, which combines all rows from two DataFrames into a new DataFrame.

Merging for Cartesian Product (Pandas >= 1.2)

The merge function in Pandas provides an efficient method for obtaining a Cartesian product. For versions 1.2 and above, use the following:

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

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

This returns a new DataFrame with all combinations of rows from df1 and df2.

Merging for Cartesian Product (Pandas < 1.2)

For earlier versions of Pandas, merge can still be used if there is a repeated key for each row. This key allows rows to be aligned for the Cartesian product:

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

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

The above is the detailed content of How to 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template