import pandas as pd
raw_data = {'FirstName': ["John", "Jill", "Jack", "John", "Jill", "Jack",],
'LastName': ["Blue", "Green", "Yellow","Blue", "Green", "Yellow"],
'Building': ["Building1", "Building1", "Building2","Building1", "Building1", "Building2"],
'Month': ["November", "November", "November", "December","December", "December"],
'Sales': [100, 150, 275, 200, 150, 150]}
frame = pd.DataFrame(raw_data, columns =raw_data.keys())
df = frame.pivot(
index=["FirstName", "LastName", "Building"],
columns="Month",
values="Sales",
)
df
I have some simple code here that generates a multi-level index dataframe that looks like this:
I'm trying to adjust the column headers (specifically December and November) and move them down, so they are not so far above the dataframe with all that whitespace underneath them. Ideally I'd also like to hide the "Month" that shows up above "building" but one problem at a time.
One solution I've tried is just hiding the index like so.
df.index.names = (None, None, None)
df
This datarame nicely moves the column headers down, so there's not as much whitespace. But now I've lost my "FirstName" and "LastName" headers since I was forced to set them to None. is there a different way of accomplishing? Can I directly control the whitespace that shows up beneath the "December" and the "November" in my first example?
Is such level of minute control possible with pandas?
Edit:
It seems like I might need to nuke my indexing by resetting it. In light of that I was wondering if there's a way to merge rows together in Pandas to preserve the visual element of indexing?
So for instance, the original data might look like this:
But, I was wondering if it's possible to merge, just visually rows in 1 column, like so:
So you can see here, that instead of "Jack" showing up in 2 rows, now there's just 1 merged cell. Indexing does something like this automatically, but if I remove indexing I'd like to mimic this feature, is that possible?