Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove blank line when displaying MultiIndex Python dataframe

I have a MultiIndex Python Dataframe displaying like this:

enter image description here

What can I do to pull up the index names ("index1", "index2") and delete the empty row above the dataframe values to get a view like this? The goal is to export the dataframe in png.

enter image description here

Here is the code that generates the first dataframe:

import pandas as pd
import numpy as np

style_valeurs = {"background-color" : "white", "color" : "black", "border-color" : "black",  "border-style" : "solid", "border-width" : "1px"}

style_col = [{"selector": "thead",
            "props": "background-color:yellow; color :black; border:3px black;border-style: solid; border-width: 1px"
            },{
            'selector':"th:not(.index_name)",
            'props':"background-color: yellow; border-color: black;border-style: solid ;border-width: 1px; text-align:left"
}]

data = np.random.rand(4,4)

columns = pd.MultiIndex.from_product([["A","B"],["C","D"]])

index = pd.MultiIndex.from_product([["x","y"],["1","2"]])

df = pd.DataFrame(data, columns = columns, index = index)

df.rename_axis(["index1","index2"], inplace = True)

df.style.set_properties(**style_valeurs).set_table_styles(style_col)
like image 760
Sabrina Avatar asked Dec 10 '25 08:12

Sabrina


1 Answers

Here is one way to do it:

df = pd.DataFrame(data, columns=columns, index=index)

# Make index as simple columns and slice them in a temporary dataframe
df = df.reset_index()
tmp = df.loc[:, ["level_0", "level_1"]]

# Replace ["x", "x", "x", "x"] with ["x", "", "x", ""]
tmp["level_0"] = [
    v if not i % tmp["level_0"].nunique() else "" for i, v in enumerate(tmp["level_0"])
]

# Fix headers
tmp.columns = pd.MultiIndex.from_tuples([("col_0", "index1"), ("col_1", "index2")])
tmp = tmp.rename(columns={"col_0": ""}).rename(columns={"col_1": ""})

# Add first two columns back with the others
new_df = pd.concat([tmp, df[["A", "B"]]]

# Define a helper function to color first two columns in yellow
def color(x):
    y = "background-color: yellow"
    df_ = pd.DataFrame("", index=x.index, columns=x.columns)
    df_.iloc[:, [0, 1]] = y
    return df_

# Style the new dataframe by hiding the index (0, 1, 2, ...), applying your styles,
# color in yellow the first two columns, and align their values to the left
new_df.style.hide(axis=0).set_properties(**style_valeurs).set_table_styles(
    style_col
).apply(color, axis=None).set_properties(subset=[""], **{"text-align": "left"})

Here is the output (I am not using a Jupyter notebook, so borders won't render, but it should work for you):

enter image description here

like image 200
Laurent Avatar answered Dec 11 '25 21:12

Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!