Pandas has a function, `melt`

, which can be thought of as the opposite of `pivot`

. It takes a table with multiple columns and converts it into a table where the column and value are represented as rows.

For example, if suppose we have the table

import pandas as pd df = pd.DataFrame({'a': [1, 2], 'b': [5, 6], 'c': [9, 10]})

a | b | c | |
---|---|---|---|

1 | 1 | 5 | 9 |

2 | 2 | 6 | 10 |

Then `melt`

can be used to convert it to a table like

variable | value | |
---|---|---|

1 | a | 1 |

2 | a | 2 |

3 | b | 5 |

4 | b | 6 |

5 | c | 9 |

6 | c | 10 |

**Task:** Write a function, `melter(df)`

which takes in the DataFrame, `df`

and melts all the columns into rows. The resulting table should have two columns, `variable`

and `value`

.

df = pd.DataFrame({'a': [1, 2], 'b': [5, 6], 'c': [9, 10]})

a | b | c | |
---|---|---|---|

0 | 1 | 5 | 9 |

1 | 2 | 6 | 10 |

variable | value | |
---|---|---|

0 | a | 1 |

1 | a | 2 |

2 | b | 5 |

3 | b | 6 |

4 | c | 9 |

5 | c | 10 |

Next Exercise