Reshaping and pivoting

During EDA, we often need to rearrange data in a dataframe in some consistent manner. This can be done with hierarchical indexing using two actions:

  • Stacking: Stack rotates from any particular column in the data to the rows. 
  • Unstacking: Unstack rotates from the rows into the column. 

We will look at the following example:

  1. Let's create a dataframe that records the rainfall, humidity, and wind conditions of five different counties in Norway:
data = np.arange(15).reshape((3,5))
indexers = ['Rainfall', 'Humidity', 'Wind']
dframe1 = pd.DataFrame(data, index=indexers, columns=['Bergen', 'Oslo', 'Trondheim', 'Stavanger', 'Kristiansand'])
dframe1

The output of the preceding snippet is as follows:

  1. Now, using the stack() method on the preceding dframe1, we can pivot the columns into rows to produce a series:
stacked = dframe1.stack()
stacked

The output of this stacking is as follows:

  1. The preceding series stored unstacked in the variable can be rearranged into a dataframe using the unstack() method:
stacked.unstack()

This should revert the series into the original dataframe. Note that there is a chance that unstacking will create missing data if all the values are not present in each of the sub-groups. Confused? Okay, let's look at two series, series1 and series2, and then concatenate them. So far, everything makes sense.

  1. Now, let's unstack the concatenated frame:
series1 = pd.Series([000, 111, 222, 333], index=['zeros','ones', 'twos', 'threes'])
series2 = pd.Series([444, 555, 666], index=['fours', 'fives', 'sixes'])

frame2 = pd.concat([series1, series2], keys=['Number1', 'Number2'])
frame2.unstack()

The output of the preceding unstacking is shown in the following screenshot:

Since in series1, there are no fours, fives, and sixes, their values are stored as NaN during the unstacking process. Similarly, there are no ones, twos, and zeros in series2, so the corresponding values are stored as NaN. Now it makes sense, right? Good. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.233