Learn Before
Concept
Reshaping Data
Data doesn't always come in the format that is very useful or helpful for performing analysis and sometimes can be re-organized to make things easier to view. Reshaping can change the relative position of certain information to help draw emphasis between different pieces of data. Consider the following example:
A B first second bar one 0.721555 -0.706771 two -1.039575 0.271860 baz one -0.424972 0.567020 two 0.276232 -1.087401
Using the .stack() method the data can be modified to look like this
first second bar one A -0.727965 B -0.589346 two A 0.339969 B -0.693205 baz one A -0.339355 B 0.593616 two A 0.884345 B 1.591431
You can also include an argument when using the stack method to specify which columns you want to stack. The same logic and usage applies when using the inverse function .unstack().
Both .stack() and .unstack() will return a sorted copy of the dataframe.
0
1
Updated 2021-06-02
Tags
Python Programming Language
Data Science