Pandas dataframe隐藏索引功能?

时间:2014-01-21 10:52:53

标签: python pandas ipython-notebook

显示pandas数据帧时是否可以隐藏索引,以便只有列名出现在表的顶部?

这需要适用于ipython notebook中的html表示和to_latex()函数(我正在使用nbconvert)。

的Ta。

5 个答案:

答案 0 :(得分:24)

正如@waitingkuo所指出的,index = False就是你所需要的。如果您想在ipython笔记本中保留漂亮的表格布局,可以使用:

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

答案 1 :(得分:16)

设置index=False

对于ipython notebook:

print df.to_string(index=False)

对于to_latex:

df.to_latex(index=False)

答案 2 :(得分:2)

我将以下单元格添加到我的笔记本中,在Jupyter 4.0.2中工作正常。

注意:它删除了第一列'任何'表,即使没有索引。

function q($input)
{
    $glue = ', ';
    $function = function ($v, $k) use (&$function, $glue) {
        if (is_array($v)) {
            $arr = [];
            foreach ($v as $key => $value) {
                $arr[] = $function($value, $key);
            }
            $result = "{" . implode($glue, $arr) . "}";
        } else {
            $result = sprintf("%s=\"%s\"", $k, var_export($v, true));
        }
        return $result;
    };
    return implode($glue, array_map($function, $input, array_keys($input))) . "\n";
}

答案 3 :(得分:2)

从0.17.1版开始,可以通过styling隐藏索引,请参见hiding the index or colums:如果df是您的数据框,那么

df.style.hide_index()

答案 4 :(得分:1)

设置index=False

例如:DataFrame.to_csv("filename", index=False)

这样可行。