8 Reshaping and pivoting

This chapter covers

  • Comparing wide and narrow data
  • Generating a pivot table from a DataFrame
  • Aggregating values by sum, average, count, and more
  • Stacking and unstacking DataFrame index levels
  • Melting a DataFrame

A data set can arrive in a format unsuited for the analysis that we’d like to perform on it. Sometimes, issues are confined to a specific column, row, or cell. A column may have the wrong data type, a row may have missing values, or a cell may have incorrect character casing. At other times, a data set may have larger structural problems that extend beyond the data. Perhaps the data set stores its values in a format that makes it easy to extract a single row but difficult to aggregate the data.

Reshaping

Get Pandas in Action now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.