Say I have the following 2 dimensional dataframe
+--------+-------------------+------------+
| Index, | Module/Line Item, | Is Module, |
+--------+-------------------+------------+
| 0, | Module 1, | True, |
| 1, | Line Item 1, | False, |
| 2, | Line Item 2, | False, |
| 3, | Module 2, | True, |
| 4, | Line Item 1, | False, |
| 5, | Line Item 2, | False |
+--------+-------------------+------------+
And I want it to turn into this:
+----------+-------------+
| Module | Line Item |
+----------+-------------+
| Module 1 | Line Item 1 |
| | Line Item 2 |
| Module 2 | Line Item 1 |
| | Line Item 2 |
+----------+-------------+
What would be the best way to accomplish that? Is there a built in way to pivot like that via pandas? I was messing with some regular python but it was very inefficient and I couldn't get it to work right.
Note there are not a set number of Line Items between Modules, and no patterns in the names. The "Is Module" column is the only indicator of whether the value is a module and should be pivoted. All Line items that appear beneath the module until the next module should belong to that module when pivoted.
This is not answered by How to pivot a dataframe because it never explains how to split a column into a hierarchy based on the values that are given in another column.