How to do it...

  1. Read in the sensors dataset and identify the variables:
>>> sensors = pd.read_csv('data/sensors.csv')
>>> sensors
  1. The only variable placed correctly in a vertical column is Group. The Property column appears to have three unique variables, Pressure, Temperature, and Flow. The rest of the columns 2012 to 2016 are themselves a single variable, which we can sensibly name Year. It isn't possible to restructure this kind of messy data with a single DataFrame method. Let's begin with the melt method to pivot the years into their own column:
>>> sensors.melt(id_vars=['Group', 'Property'], var_name='Year') 
.head(6)
  1. This takes care of one of our issues. Let's use the pivot_table method to pivot the Property column into new column names:
>>> sensors.melt(id_vars=['Group', 'Property'], var_name='Year') 
.pivot_table(index=['Group', 'Year'],
columns='Property', values='value')
.reset_index()
.rename_axis(None, axis='columns')
..................Content has been hidden....................

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