How to Combine Pandas DataFrames with Overlapping Columns?

Linda Hamilton
Release: 2024-10-25 03:23:02
Original
895 people have browsed it

How to Combine Pandas DataFrames with Overlapping Columns?

How to Combine Pandas Data Frames Based on a Shared Column

Problem:

When attempting to combine two pandas data frames using the DataFrame.join() method, an error is encountered: "Columns overlap."

Data Frames:

  • restaurant_ids_dataframe: Contains information about restaurant IDs, categories, addresses, etc.
  • restaurant_review_frame: Includes review data such as dates, stars, user IDs, etc.

Attempted Code:

<code class="python">restaurant_review_frame.join(other=restaurant_ids_dataframe, on='business_id', how='left')</code>
Copy after login

Error:

<code class="text">Exception: columns overlap: Index([business_id, stars, type], dtype=object)</code>
Copy after login

Solution:

To resolve the error and combine the data frames, use the merge() method instead of join():

<code class="python">import pandas as pd
result = pd.merge(restaurant_ids_dataframe, restaurant_review_frame, on='business_id', how='outer')</code>
Copy after login

By default, merge() uses an outer join, which combines all rows from both data frames. The on argument specifies the column used to perform the merging operation.

Suffixes for Overlapping Columns:

Since both data frames have a column named stars, the merged data frame will contain two columns: stars_x and stars_y. To customize these suffixes, use the suffixes argument:

<code class="python">result = pd.merge(..., suffixes=('_restaurant_id', '_restaurant_review'))</code>
Copy after login

This will rename the stars columns to stars_restaurant_id and stars_restaurant_review in the merged data frame.

The above is the detailed content of How to Combine Pandas DataFrames with Overlapping Columns?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!