Pandas GroupBy - Unstack

Pandas GroupBy - Unstack

Pandas' groupby followed by unstack can be a powerful combination. The groupby method groups the DataFrame using a particular column, and unstack reshapes the result. This is useful when you want to pivot your data from a long format to a wide format.

Here's a step-by-step guide on how to use groupby followed by unstack:

  • Setup: First, ensure you have Pandas imported and your DataFrame is ready:
import pandas as pd data = { 'Date': ['2021-01-01', '2021-01-01', '2021-01-02', '2021-01-02'], 'City': ['New York', 'Los Angeles', 'New York', 'Los Angeles'], 'Temperature': [55, 75, 56, 74], 'Humidity': [56, 50, 55, 48] } df = pd.DataFrame(data) print(df) 

This would produce:

 Date City Temperature Humidity 0 2021-01-01 New York 55 56 1 2021-01-01 Los Angeles 75 50 2 2021-01-02 New York 56 55 3 2021-01-02 Los Angeles 74 48 
  • GroupBy and Unstack:

Now, let's say you want to pivot the DataFrame so that the cities become columns and the values are the temperatures. You can achieve this with groupby followed by unstack:

result = df.groupby(['Date', 'City'])['Temperature'].mean().unstack() print(result) 

This will produce:

City Los Angeles New York Date 2021-01-01 75 55 2021-01-02 74 56 
  • Multiple Columns:

If you want to unstack multiple columns (e.g., both Temperature and Humidity), you can use:

result = df.groupby(['Date', 'City']).mean().unstack() print(result) 

The output:

 Temperature Humidity City Los Angeles New York Los Angeles New York Date 2021-01-01 75 55 50 56 2021-01-02 74 56 48 55 

The result now shows both Temperature and Humidity as unstacked columns for each city.

Using groupby and unstack, you can pivot your data to represent it in various structures, making it easier to analyze and visualize.


More Tags

pageviews css-float oop angularjs httpwebresponse react-16 lightgbm payment-gateway tags templating

More Programming Guides

Other Guides

More Programming Examples