Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the stack () method, unstack () method and pivot () method in python DataFrame

Shulou Source: shulou.com Published: 2022-06-01 01:18:42 10月03日 Update

This article mainly explains "how to use stack() method, unstack() method and pivot() method in python DataFrame". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "how to use stack() method, unstack() method and pivot() method in python DataFrame" together!

1.stack()

stack() is used to convert the column index to the innermost row index. This description is more abstract. It is easy to understand by looking at the example:

Prepare a set of data and double index it.

import pandas as pddata = [['Class',' a1', 123, 224, 254], ['Class',' a2', 234, 135, 444], ['Class',' a3', 345, 241, 324], ['Class B',' b1', 112, 412, 466], ['Class B',' b2', 224, 235, 345], ['Class B',' b3', 369, 214, 352], ['Class C',' c1', 236, 251, 485], ['Class C',' c2', 378, 216, 515], ['Class C',' c3', 135, 421, 312], ['Class D',' d1', 306, 325, 496], ['Class D',' d2', 147, 235, 524], ['Class D',' d3', 520, 222, 267]]df = pd.DataFrame (data=data, columns=['category',' number','A indicators','B indicators','C indicators'])df = df.set_index([' category','number'])print(df)

df = df.stack()print(df)

As shown in the figure, the three columns of indicator A, indicator B and indicator C outside the index column are successfully placed in the same column.

df is no longer a DataFrame, but a Series object.

print(type(df))

The index column of the Series is different from the index column of the original DataFrame, but on the basis of the index column of the original DataFrame, the part merged from the right is added:

print(df.index)

Values are:

print(df.values)

2. unstack()

Unstack is the reverse of stack.

Based on the above example code, continue to call unstack() method on df above:

df1 = df.unstack()print(df1)

You can see unstack changing back to its original form.

3. pivot()

Here are some minor adjustments to the data from the example above:

Do not set multiple indexes

import pandas as pddata = [['Class',' 1', 123, 224, 254], ['Class',' 2', 234, 135, 444], ['Class',' 3', 345, 241, 324], ['Class B',' 1', 112, 412, 466], ['Class B',' 2', 224, 235, 345], ['Class B',' 3', 369, 214, 352], ['Class C',' 1', 236, 251, 485], ['Class C',' 2', 378, 216, 515], ['Class C',' 3', 135, 421, 312], df = pd.DataFrame(data=data, columns=['class',' number','indicator',' indicator'])print(df)

df2 = df.pivot(index ='number ', columns ='category', values ='A indicators') print(df2)

Index and columns refer to setting the value of that column to index and setting the value of that column to columns, respectively. values refers to the indicators that the table is intended to represent.

df3 = df.pivot(index ='category ', columns ='number', values ='A indicators') print(df3)

Thank you for reading, the above is "python DataFrame in the stack() method, unstack() method and how to use the pivot() method" content, after the study of this article, I believe that we have a deeper understanding of python DataFrame in the stack() method, unstack() method and pivot() method how to use this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Methods metrics categories indexes learning above content fundamentals data examples success code examples inner layer right objects that is ideas situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux OPPO Reno vpn NVidia