Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the long format into the wide format in Python data

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people do not know what to do about how to change the long format of Python data into a wide format. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Pivot

Pandas uses version 0.22

The inverse operation of melt () corresponds to pivot () in Pandas, which is also a top-level function in design. The project location is as follows:

Pandas

| |

Pivot ()

It can transform the long format table into a wide format.

Pivot is interpreted as: axis consisting of a short shaft that supports something that turns contains a shaft with a short handle to support the rotation of the object. How do you understand it?

Figure 1

Figure 2

Figures 1 through 2 are the pivot operations in Pandas.

Function prototype

Main parameters:

Index indicates which column becomes the index of the new DataFrame, paying attention to which, not which

Columns indicates which column becomes columns

Values indicates which columns become the data fields of the new DataFrame, and if not, defaults to columns other than those that are specified index and columns.

The transformation from figure 1 to figure 2 above is as follows, the values parameter is not specified, and the other columns are all listed hierarchically. Obviously, the column is widened to a wide format.

If you only want to get a series, such as the baz series, do the following:

Figure 2 changes as follows:

Abnormal

An exception may occur in the pivot operation in practice because the value chosen as the crossing point between index and columns should not be the only value.

As mentioned above, the value of [one,A] is 1 or 2, which is not unique. Therefore, an exception is reported when you call the following operation.

The exception is as follows

These are the details of the use of pivot. Note that the pivot function does not have aggregation capabilities. Pivot_table () in pandas provides aggregate functions to implement aggregation functions.

Although it is only a simple function, it can quickly and powerfully analyze the data. If you want to use it thoroughly, you need to think more and apply it to the actual scene as much as possible.

After reading the above, have you mastered how to change the long format of Python data into a wide format? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report