Reshape DataFrame using stack() and unstack() functions in Pandas
Pandas is an advanced data analysis tool or package extension in Python. It is highly recommended to use Pandas when we have data in SQL tables, spreadsheets, or heterogeneous columns.
This article discusses the basic concepts of stacking and unstacking in Pandas. In Pandas, stacking and unstacking are widely used to change the shape of the DataFrame under consideration.
Let's see this method in action. First, we'll create a dummy DataFrame dates_data
with a few rows.
import pandas as pd
index = pd.date_range("2013-1-1", periods=100, freq="30Min")
dates_data = pd.DataFrame(data=list(range(100)), columns=["value"], index=index)
dates_data["value2"] = "Alpha"
dates_data["value2"].loc[0:10] = "Beta"
The above code block creates a DataFrame dates_data
containing the date and two columns named value
and value2
. To view the entries in the data, we use the following code:
print(dates_data)
Output:
value value2
2013-01-01 00:00:00 0 Beta
2013-01-01 00:30:00 1 Beta
2013-01-01 01:00:00 2 Beta
2013-01-01 01:30:00 3 Beta
2013-01-01 02:00:00 4 Beta
... ... ...
2013-01-02 23:30:00 95 Alpha
2013-01-03 00:00:00 96 Alpha
2013-01-03 00:30:00 97 Alpha
2013-01-03 01:00:00 98 Alpha
2013-01-03 01:30:00 99 Alpha
As we can see, we have 100 different entries, each with the same time set after an interval of 30 minutes.
value
Additionally, two additional columns named and are created value2
where we set some values to numbers and other values to Alpha
or Beta
.
stack()
Sum unstack()
Function in Pandas
We can change the DataFrame named with the help of two functions in Pandas named stack()
and . This function helps us to change the direction of the DataFrame so that the rows become columns and the columns become rows accordingly.unstack()
dates_data
value
We will try to convert the and in the DataFrame value2
to rows and the values in it to entries in the row.
Use unstack()
the function to transform our DataFrame
Order:
dates_data = dates_data.unstack()
print(dates_data)
Output:
value 2013-01-01 00:00:00 0
2013-01-01 00:30:00 1
2013-01-01 01:00:00 2
2013-01-01 01:30:00 3
2013-01-01 02:00:00 4
...
value2 2013-01-02 23:30:00 Alpha
2013-01-03 00:00:00 Alpha
2013-01-03 00:30:00 Alpha
2013-01-03 01:00:00 Alpha
2013-01-03 01:30:00 Alpha
Length: 200, dtype: object
Now that we have successfully transformed our data, we now have the columns as row entries in our data.
Use unstack()
the function to transform our DataFrame
Order:
dates_data = dates_data.stack()
print(dates_data)
Output:
2013-01-01 00:00:00 value 0
value2 Beta
2013-01-01 00:30:00 value 1
value2 Beta
2013-01-01 01:00:00 value 2
...
2013-01-03 00:30:00 value2 Alpha
2013-01-03 01:00:00 value 98
value2 Alpha
2013-01-03 01:30:00 value 99
value2 Alpha
Length: 200, dtype: object
The column values are now stacked as rows in our DataFrame.
So, with the help of in Pandas unstacking 技术
, we can effectively filter the data as and when required and transform the appearance of the DataFrame to visualize the data in a better way.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Plotting Line Graph with Data Points in Pandas
Publish Date:2025/04/12 Views:65 Category:Python
-
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get