如何在数据框中居中对齐 headers 和值,以及如何在数据框中删除索引
How to center align headers and values in a dataframe, and how to drop the index in a dataframe
我有以下数据框:
import pandas as pd
df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
'number': [1, 2]})
df
如何 center-align 列 titles/headers 和数据框中的值,以及如何删除数据框中的索引(值为 0 和 1 的列)?
尝试IPython.display
from IPython.display import HTML
HTML(df.to_html(index=False))
已找到答案。这应该可以解决 center-align headers 和值并隐藏索引的问题:
df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()
df.style.set_properties(subset=["Feature", "Value"], **{'text-align': 'center'})
我有以下数据框:
import pandas as pd
df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
'number': [1, 2]})
df
如何 center-align 列 titles/headers 和数据框中的值,以及如何删除数据框中的索引(值为 0 和 1 的列)?
尝试IPython.display
from IPython.display import HTML
HTML(df.to_html(index=False))
已找到答案。这应该可以解决 center-align headers 和值并隐藏索引的问题:
df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()
df.style.set_properties(subset=["Feature", "Value"], **{'text-align': 'center'})