Stack
Stack¶
The stack function takes a dataframe and brings down each column. It can be tough to explain it so let’s just see what it looks like.
In [9]:
#Create the GDP object
gdp = pd.DataFrame([[.03, .04, .03],
[.05, .03, .01],
[.02, .02, .07],
[.03, .01, .02]], index=["Q1", "Q2", "Q3", "Q4"], columns=['Country A', "Country B", "Country C"])
print("Before stack:")
print(gdp)
print()
#Stack
gdp = gdp.stack()
print("After stack:")
print(gdp)
So you will see that we went from 4x3 dataframe to 12 values. One result of using this is that we now have a new type of index. Let's check out what we get out.
In [10]:
#Print out the GDP index after stacking
print(gdp.index)