设置
我有一个dataframe的例子:
import pandas as pd
df = pd.DataFrame.from_dict({'A':[1,2],'B':[3,4]})
我做了什么
使用to_latex(),我可以将这个数据框打印到一个latex表。但是,我不希望包含索引列。index_names
关键字应该有帮助。根据documentation:
**index_names:**bool,默认True
打印索引的名称。
我愿意:
df.to_latex(index_names=False)
我得到了什么
\begin{tabular}{lrr}
\toprule
{} & A & B \\
\midrule
0 & 1 & 3 \\
1 & 2 & 4 \\
\bottomrule
\end{tabular}
这与预期不符:0和1不应该出现在两行的开头(在\midrule
和\bottomrule
之间)。我还尝试了df.to_latex()
,它给出的输出与我以前得到的相同。
问题
似乎设置index_names=False
对输出没有任何影响。Link to a Google Colab notebook confirming this result。
如何打印出没有index列的Pandas Dataframe ?
2条答案
按热度按时间nuypyhwy1#
您需要
index=False
,而不是index_names=False
:index_names=False
所做的是删除列标题下包含索引级别名称的行。这只会发生在 * 是 * 一个索引名称。请参见下面带有idx
的行:lg40wkob2#
pandas
的新接口使用styler