How to use stack and pivot to achieve data Perspective in Pandas
This article mainly explains "Pandas how to use stack and pivot to achieve data perspective", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Pandas how to use stack and pivot to achieve data perspective" bar!
Catalogue
Preface
First, multi-dimensional index data are obtained through statistics.
Second, use unstack to realize two-dimensional perspective of data.
Use pivot to simplify perspective
IV. The grammar of stack, unstack and pivot
1.stack
2.unstack
3.pivot
Preface
Recently, the author is studying Pandas data analysis and turning his study notes into a series of articles. This section mainly records the use of stack and pivot for data perspective in Pandas.
First, multi-dimensional index data are obtained through statistics.
For statistical scenarios of unusual scenarios, specify multiple dimensions and calculate the aggregated metrics
Example: statistics to get the "movie rating data set", each month of each score is scored how many times: (month, score 1-5, times)
Import pandas as pdimport numpy as np%matplotlib inlinedf=pd.read_csv (". / datas/ml-1m/ratings.dat", sep= "::", engine='python', names='UserID::MovieID::Rating::Timestamp'.split ("::"), header=None) df.head () # converts the timestamp to a specific time df ['padate'] = pd.to_datetime (df ["Timestamp"] Unit='s') df.head () df.dtypes# implements data statistics # for data in this format I want to see the trend of the number of times by month, different ratings, there is no way to achieve, need to convert the data into each score is a column can be achieved. Df_group=df.groupby ([df ["padate"] .dt.month, "Rating"]) ["UserID"] .agg (pv=np.sum) df_group.head (20)
Second, use unstack to realize two-dimensional perspective of data.
Objective: to draw a picture to compare the quantitative trend of different scores by month.
Df_stack=df_group.unstack () df_stackdf_stack.plot () # unstack and stack are reciprocal operations df_stack.stack () .head (20)
Use pivot to simplify perspective
The pivot method is equivalent to creating a hierarchical index on df using set_index, and then calling unstack
Df_group.head (20) df_reset=df_group.reset_index () df_reset.head () df_pivot=df_reset.pivot ("padate", "Rating", "pv") df_pivot.head () df_pivot.plot ()
4. The grammar 1.stack of stack, unstack and pivot
Stack:DataFrame.stack (level=-1,dropna=True), which turns column into index, similar to turning horizontal books into vertical.
Level=-1 represents the innermost layer of a multi-tier index, and the corresponding layer of the multi-tier index can be specified by = = 0, 1, 2.
2.unstack
Unstack:DataFrame.unstack (level=-1,fill_value=None), which turns index into column, similar to turning vertical books into horizontal ones
3.pivot
Pivot:DataFrame.pivot (index=None,columns=None,values=None), specifying index,columns,values to achieve 2D perspective
Thank you for your reading, the above is the content of "how Pandas uses stack and pivot to achieve data perspective". After the study of this article, I believe you have a deeper understanding of how Pandas uses stack and pivot to achieve data perspective, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!