Home > Backend Development > Python Tutorial > How to Reshape Long to Wide Data in Pandas Using Two Variables?

How to Reshape Long to Wide Data in Pandas Using Two Variables?

Susan Sarandon
Release: 2024-10-29 18:05:02
Original
959 people have browsed it

How to Reshape Long to Wide Data in Pandas Using Two Variables?

Pandas Reshaping Long to Wide by Two Variables

Manipulating data between long and wide formats is a common task in data analysis. In Python's Pandas library, melt and stack/unstack operations are commonly used for this purpose. However, certain scenarios may arise where a more straightforward approach is desired.

One such scenario is when reshaping data that includes two variables (e.g., a numeric variable like sales and a categorical variable like product) into a wide format. Using melt/stack/unstack methods alone may not provide the desired output.

In this example, we have "long" data with the following columns: Salesman, Height, product, and price. Our goal is to reshape this data into a "wide" format with columns for each unique product, including its corresponding price.

Salesman  Height   product      price
  Knut      6        bat          5
  Knut      6        ball         1
  Knut      6        wand         3
  Steve     5        pen          2
Copy after login

To accomplish this, we can leverage Pandas' pivot function, which provides a convenient way to create pivot tables. We specify the index column (Salesman), pivot columns (obs), and values column (price).

Here's the Python code to reshape the data:

<code class="python">wide_df = df.pivot(index='Salesman', columns='product', values='price')</code>
Copy after login

This will produce the desired "wide" format:

Salesman  Height    product_1  price_1  product_2 price_2 product_3 price_3  
  Knut      6        bat          5       ball      1        wand      3
  Steve     5        pen          2        NA       NA        NA       NA
Copy after login

The above is the detailed content of How to Reshape Long to Wide Data in Pandas Using Two Variables?. 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