SantoshGupta7
SantoshGupta7

Reputation: 6197

How to display text at the top a cell

I see that text can be aligned to the left or to the right.

There are situations where one column has a lot of text and then all other columns have their values in the vertical middle of the column.

df.style.set_properties(**{'text-align': 'right'})

I tried

df.style.set_properties(**{'text-align': 'top'})

And it didn't work

It seems that this is borrowed from css so I also tried

df.style.set_properties(**{'vertical-align': 'top'})

Upvotes: 2

Views: 1599

Answers (1)

lfalin
lfalin

Reputation: 4304

The CSS pairing you want is: "vertical-align": "text-top"

Example:

import pandas as pd

pd.set_option('display.max_colwidth', None)

d = {'A': [1, "This is a medium long value"], 'B': [1, "This is a medium long value"], 'C': [1, "This is a medium long value"], 'D': [1, "This is a medium long value"], 
     'E': [1, "This is a medium long value"], 'F': [1, "This is a medium long value"], 'G': [1, "This is a medium long value"], 'H': [1, "This is a medium long value"], 
     'X': [1, "This is a really really really long value that goes on and on and on and on and on and on and on and on."]}
df = pd.DataFrame(data=d)

df.style.set_properties(**{"vertical-align": "text-top"})

Without "vertical-align": "text-top":

data not aligned

With "vertical-align": "text-top"

data aligned

Upvotes: 2

Related Questions