Data science

Stage 1 · Lesson 7 of 7

Reshaping between long and wide

Python for data: NumPy and pandas

Wide data has one column per month or category, which is how people like to read tables. Long data has observation and a column saying which month or category it belongs to, which is what plotting libraries and models usually want.

Converting between them is a daily step. uses melt to go long and pivot or pivot_table to go wide; tidyr uses pivot_longer and pivot_wider. Before reshaping, decide what one row should represent in the result.

Common trappivoting wide when two rows share the same row and column labels: pandas pivot raises an error, pivot_table quietly averages them, and tidyr warns and packs them into lists, so check for duplicates first.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking Python Fundamentals

    Design Gurus · Beginner · $50

    Solid Python basics first: data types, collections, functions and files, the ground pandas stands on

  • CourseFree
    Kaggle Learn

    Kaggle · Beginner

    Free: the Python and Pandas courses, short lessons with exercises that run in the browser

Practise

  • Pick one dataset on a topic you care about and repeat every operation in this stage on it

Keep handy

  • The indexing, merging and reshaping pages answer most "how do I" questions

  • The absolute beginners guide, then the reference when an array does something unexpected

Also worth your time

Mark this stage as not done?