Reputation: 2232
here' a question on transforming a DataFrame. After creating a DataFrame from a pivot table and applying .unstack(), I came up with a table like this
Amount Color Red Yellow Blue Green Date 2006-01 56 41 15 10 2006-01 51 23 26 36 2006-01 36 54 15 43
Now, my question: How can I delete the rows/columns "Amount" and "Color" above the main index ("Date")? This is what I need:
Red Yellow Blue Green Date 2006-01 56 41 15 10 2006-01 51 23 26 36 2006-01 36 54 15 43
Also, it would be good to know how to rename the index "Date" as I have to merge two tables where in one case the time is named "Date" and in the other "Close Date".
Thanks for your help!
Upvotes: 2
Views: 66
Reputation: 11381
You can get rid of your top-level columns by just selecting the one top-level column value:
df = df["Amount"]
Color
isn't the name of a column - it's the name of the row of column names. If you don't want your column levels to have names, you can set them to None
:
df.columns.names = [None]
If you haven't gotten rid of the top-level column, you'll have to set df.columns.names = [None, None]
since there will still be two levels of column names.
Upvotes: 3