如何在单元格顶部显示文本

How to display text at the top a cell

我看到文本可以左对齐或右对齐。

有些情况下,一列有很多文本,然后所有其他列的值都位于该列的垂直中间。

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

我试过了

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

它没有用

这个好像是从css那里借来的所以我也试了

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

您想要的CSS配对是:"vertical-align": "text-top"

示例:

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"})

没有"vertical-align": "text-top":

"vertical-align": "text-top"