Given that I have the following Pandas DataFrame:
df =
| Lttr | Day | Color | Num |
|---|---|---|---|
| A | Mon | Red | One |
| A | Tue | Blu | One |
| A | Wed | Grn | One |
| A | Wed | Grn | Two |
How could I best go about re-arranging it to something like:
| Lttr | Mon | Tue | Wed |
|---|---|---|---|
| A | Red | Blu | Grn |
Given that I have the following Pandas DataFrame:
df =
| Lttr | Day | Color | Num |
|---|---|---|---|
| A | Mon | Red | One |
| A | Tue | Blu | One |
| A | Wed | Grn | One |
| A | Wed | Grn | Two |
How could I best go about re-arranging it to something like:
| Lttr | Mon | Tue | Wed |
|---|---|---|---|
| A | Red | Blu | Grn |
Use:
(df.pivot_table(index='Lttr', columns='Day', values='Color', aggfunc='first')
.reset_index()
.rename_axis(columns=None)
)
Output:
Lttr Mon Tue Wed
0 A Red Blu Grn