Tidy Data Principles for your DataFrame

Data science requires a solid understanding of tidy data principles. A good data scientist can recognize the difference between tidy and untidy data. It takes a bit of practice, but following two principles can help keep it simple.

The Two Tidy Data Principles

  1. Each variable has a separate column.
  2. Each row represents a separate observation.

Shown below are two images. Each image represents a sample of weather observation data. One is tidy, the other is not. Follow the aforementioned tidy data principles to understand.

untidy data
This data sample is untidy.
tidy data principles
This data sample is tidy.

In the first image of untidy data, the single column ‘variable’ contains all the variables. Consequently, several rows will hold the same observation, but for each variable. The second image corrects the untidy data because there are separate column for each variable.

Use the first steps of data wrangling when presented with a new data set. This is the best way to detect if the data is already tidy or not.

Untidy Data Isn’t Always Bad

The tidy data principles were adopted from a paper by Hadley Wickham as standard way to organize data for analysis. But sometimes reshaping data away from this standard can make it present better for reports. It all depends on the data.

Think of a tidy data set as the standard starting point for analysis and visualization. It is fine to reshape data if needed for a certain purpose.

Melt Away The Tidy Data

Melting data is the process of turning columns into rows. In the above images, the tidy data can be melted with the Pandas pd.melt() method. This is how the tidy image reshapes to the untidy image. Assume the tidy dataframe is called airquality, and the untidy one will be called airquality_melt.

airquality_melt = pd.melt(airquality, id_vars=[‘Month’, ‘Day’])

Notice the parameters id_vars. This is a list of columns to not melt. The parameter value_vars specifies columns to melt. Every column not in id_vars will melt by default, if value_vars is not used.

Give Descriptive Names for ‘Variable’ and ‘Value’

Refer to the ‘variable’ and ‘value’ columns in the untidy data image above. Accomplish this with var_name and value_name parameters.

airquality_melt = pd.melt(airquality, id_vars=[‘Month’, ‘Day’], var_name=’measurement’, value_name=’reading’)

Pivot is Opposite of Melt

Use the pivot_table method to get the melted version of the dataframe back to its original state.

airquality_pivot = airquality_melt.pivot_table(index=[‘Month’, ‘Day’], columns=’measurement’, values=’reading’)

# Reset the index of airquality_pivot
airquality_pivot = airquality_pivot.reset_index()

Pivot will not work if there are duplicate rows of observations. Duplicates can be dealt with by providing an aggregate function. Use np.mean for the aggregate function to reduce duplicates with the mean value.

airquality_pivot = airquality_dup.pivot_table(index=[‘Month’, ‘Day’], columns=’measurement’, values=’reading’, aggfunc=np.mean)

# Reset the index of airquality_pivot
airquality_pivot = airquality_pivot.reset_index()

Leave a Reply

Your email address will not be published. Required fields are marked *