Import NumPy as NP import pandas as PD from pandas import series,dataframe ' If copied code, error syntaxerror:invalid character
In identifier, there is a space for the Chinese symbol in the copied code. "DATA=PD." Dataframe (Np.arange (6). Reshape ((3,2)), INDEX=PD. Index ([' A ', ' B ', ' C '],name= ' state '), COLUMNS=PD. Index ([' I ', ' II '],name= ')] Print (data) ' number I II state a 0 1 B 2 3 C 4 5 ' ' Result=data.unstack () print (result) ' "number state I a 0 B 2 C 4 II
A 1 B 3 C 5 Dtype:int32 ' "' is used to stack and unstack two functions when data rearrangement is performed with pandas.
Stack means stacking, piling, unstack that is "do not stack", how to understand and distinguish it.
There are two types of hierarchical structures for common data, one is the table, one is the "curly braces", that is, the following L Two forms: table in the row and column direction of the index (similar to dataframe), the curly braces structure only "column Direction" index (similar to the Hierarchical series), The structure is more inclined to stack (series-stack, convenient memory). The stack function changes the data from the table structure to the curly brace structure, which turns the row index into a column index, whereas the Unstack function changes the data from the curly brace structure to the table structure, which turns the column index of one layer into a row index. Example: The print results are as follows: The stack function is used to transform the row index of data [' One ', ' two ', ' three '] into a column index (the second layer), and a hierarchical series is obtained (data2Using the Unstack function, the second-tier index of DATA2 is transformed into a row index (the default, which can be changed), and then the Dataframe (DATA3) is obtained. ' Data=dataframe (Np.arange (6). Reshape ((2,3)), INDEX=PD. Index ([' Street1 ', ' Street2 ']) \, COLUMNS=PD. Index ([' One ', ' two ', ' three ']) print (data) ' One two three street1 0 1 2 Street2 3 4 5 ' ' Print ('-----------------------------------------\ n ') Data2=data.stack () print (data2) ' Street1 one 0 t Wo 1 three 2 Street2 one 3 two 4 three 5 dtype:int32 ' Print ('-------- ---------------------------------\ n ') Data3=data2.unstack () print (data3) ' ' One Two three Street1 0 1
2 Street2 3 4 5 ""