Merge cells with pandas

Question:

I want to remove duplicated cells with merge them all because they point out subelements.
For example I have a df like this:

|   | Customer ID | Category      | VALUE   |
| -:|:----------- |:------------- | -------:|
| 0 | HETO90      | Baby Sets     |  1000.0 |
| 1 | HETO90      | Girls Dresses |  5356.0 |
| 2 | HETO90      | Girls Jumpers |  2822.0 |
| 3 | HETO90      | Girls Top     | 13398.0 |
| 4 | HETO90      | Shorts        |  7590.0 |

I just want to merge HET090 to the one. Like this:

|   | Customer ID | Category      | VALUE   |
| -:|:----------- |:------------- | -------:|
| 0 |             | Baby Sets     |  1000.0 |
| 1 |             | Girls Dresses |  5356.0 |
| 2 | HETO90      | Girls Jumpers |  2822.0 |
| 3 |             | Girls Top     | 13398.0 |
| 4 |             | Shorts        |  7590.0 |
Asked By: yigitozmen

||

Answers:

In pandas the inner most index must label each row.

df = df.set_index('Customer ID', append=True).swaplevel(0,1)

Output:

                    Category    VALUE
Customer ID                          
HETO90      0      Baby Sets   1000.0
            1  Girls Dresses   5356.0
            2  Girls Jumpers   2822.0
            3      Girls Top  13398.0
            4         Shorts   7590.0
Answered By: Scott Boston

Can we get the above dataframe without changing the column

Customer ID
as index?

Answered By: Satyam Rout
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.