Issue
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 |
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.