Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在数据框中居中对齐标题和值,以及如何在数据框中删除索引_Python_Pandas_Jupyter Notebook_Ipython - Fatal编程技术网

Python 如何在数据框中居中对齐标题和值,以及如何在数据框中删除索引

Python 如何在数据框中居中对齐标题和值,以及如何在数据框中删除索引,python,pandas,jupyter-notebook,ipython,Python,Pandas,Jupyter Notebook,Ipython,我有以下数据帧: import pandas as pd df = pd.DataFrame({'text': ['foo foo', 'bar bar'], 'number': [1, 2]}) df 如何将列标题/标题和数据框中的值居中对齐,以及如何将索引(值为0和1的列)放到数据框中?请尝试IPython.display from IPython.display import HTML HTML(df.to_html(index=False)) 试

我有以下数据帧:

import pandas as pd

df = pd.DataFrame({'text': ['foo foo', 'bar bar'],
                 'number': [1, 2]})
df

如何将列标题/标题和数据框中的值居中对齐,以及如何将索引(值为0和1的列)放到数据框中?

请尝试IPython.display

from IPython.display import HTML
HTML(df.to_html(index=False))
试试IPython.display

from IPython.display import HTML
HTML(df.to_html(index=False))

找到了这个问题的答案。这样做可以使标题和值居中对齐并隐藏索引:

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()

找到了答案。这样做可以使标题和值居中对齐并隐藏索引:

df1 = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
df1.set_properties(**{'text-align': 'center'}).hide_index()

为了删除索引,您必须设置一个不同的索引,除非您想打印或保存到文件中,否则您可以将有关对齐的相关参数设置为
False
,您想在哪里对齐它?Hi@Oleg,我想对齐标题和值,使其位于每个“框”的中心我发现这个
df.style.set_properties(**{'text-align':'center'})
可以将值居中对齐,但是如何将标题居中对齐呢?听起来,您想要的是编辑您在编写
df
时使用的笔记本的对齐设置,这类似于编写
display(df)
正如这里所提到的,您也应该查看熊猫的文档:好的,非常感谢@Oleg。事实上,我找到了这个问题的答案,并在下面发布了它。为了删除索引,你必须设置一个不同的索引,除非你想打印它或保存到一个文件,然后你可以将有关对齐的参数设置为
False
,你想在哪里对齐它?Hi@Oleg,我想将标题和值对齐,使其位于各自“框”的中心。我发现这是
df.style.set_properties(**{'text-align':'center'})
它将使值居中对齐,但是我如何将标题居中对齐呢?听起来你想要的是编辑你在写
df
时使用的笔记本的对齐设置,这类似于写
display(df)
,正如这里提到的,你也应该查看熊猫的文档:好的,非常感谢@Oleg。事实上,我找到了这个问题的答案,并将其发布在下面。谢谢@Sujit Dhamale。将标题和值居中对齐,使其在数据框中的每个“框”中居中如何?我发现了这个
df.style.set_属性(**{'text-align':'center'})
这将使值居中对齐,但如何将标题居中对齐?谢谢@Sujit Dhamale。如何居中对齐数据框中每个“框”中的标题和值?我发现这是
df.style.set_properties(**{'text-align':'center'})
将值居中对齐,但是我如何将标题居中对齐呢?这是可行的,但结果不是一个数据帧,我也不确定它是什么(Styler?)。我们如何做同样的事情,但输出一个数据帧?这是可行的,但结果不是一个数据帧,我不知道它是什么(Styler?)。我们如何做同样的事情,但输出一个数据帧?