First, take note that the state names are in the index of the DataFrame. These states are correctly placed vertically and do not need to be restructured. It is the column names that are the problem. The stack method takes all of the column names and reshapes them to be vertical as a single index level:
>>> state_fruit.stack() Texas Apple 12
Orange 10
Banana 40
Arizona Apple 9
Orange 7
Banana 12
Florida Apple 0
Orange 14
Banana 190
dtype: int64
Notice that we now have a Series with a MultiIndex. There are now two levels in the index. The original index has been pushed to the left to make room for the old column names. With this one command, we now essentially have tidy data. Each variable, state, fruit, and weight is vertical. Let's use the reset_index method to turn the result into a DataFrame:
Instead of directly changing the columns attribute, it's possible to use the lesser-known Series method rename_axis to set the names of the index levels before using reset_index: