pandas 来自熊猫的 JS 数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15079118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:40:39  来源:igfitidea点击:

JS dataTables from pandas

pythondatatablespandas

提问by Michael WS

I want to use pandas dataFrames with dataTables. I cannot figure out how to initialize the table without an id.

我想将Pandas数据帧与数据表一起使用。我无法弄清楚如何在没有 id 的情况下初始化表。

Is there any way to set the id in the table tag when I call df.to_html()?

当我调用 df.to_html() 时,有没有办法在 table 标签中设置 id?

回答by Kevin Bishop

You could try this:

你可以试试这个:

df.to_html(classes = 'my_class" id = "my_id')

It's like a SQL injection basically.
Pandas' to_html function uses double quotes around the class. You can use single quotes to define the classes argument, and put double quotes inside them to end pandas' class. Then put opening double quotes around your id name but let pandas' close those double quotes for you. The output will be the following:

基本上就像 SQL 注入。
Pandas 的 to_html 函数在类周围使用双引号。您可以使用单引号来定义 classes 参数,并在其中放置双引号以结束 pandas 的类。然后在您的 id 名称周围加上双引号,但让 pandas 为您关闭这些双引号。输出如下:

'<table border="1" class="dataframe my_class" id = "my_id">...'

Hope that helps.

希望有帮助。

回答by Andy Hayden

I don't think this behaviour is available in to_html, but one way is to just insert it in manually:

我不认为这种行为在 中可用to_html,但一种方法是手动将其插入:

In [11]: df = pd.DataFrame([1])

In [12]: s = df.to_html()

In [13]: print (s[:7] + 'id="my_dfs_id" ' + s[7:])
<table id="my_df_id" border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>0</strong></td>
      <td> 1</td>
    </tr>
  </tbody>
</table>

You could put this behaviour into a function:

您可以将此行为放入函数中:

def df_to_html_with_id(df, id):
    s = df.to_html()
    return s[:7] + 'id="%s" ' % id + s[7:]

Example usage: df_to_html_with_id(df, "hello").

用法示例:df_to_html_with_id(df, "hello").

回答by citynorman

I took a sligthly different approach and decided to initialize by CSS class which had the benefit that all the pandas tables became DataTables. You can add another class if you want a more refined control with different options

我采取了一种稍微不同的方法,并决定通过 CSS 类进行初始化,这有利于所有 Pandas 表都变成了 DataTables。如果您想要使用不同选项进行更精细的控制,可以添加另一个类

$(document).ready(function(){
    $('.dataframe').DataTable();
});

回答by Chandan Purohit

Although the accepted answer works amazingly, here is what i did to apply id and also some classes to heading of tables.

尽管接受的答案效果惊人,但这是我为将 id 和一些类应用于表标题所做的工作。

html = df.to_html().replace('<table','<table class="tableBoot" id="myTable"')

This works because to_html just returns a string and we can use python replace method of string objects to replace any part with anything else( note that i used only the opening '<'). I used this to include styles to <thead>ie. headings section of the table!

这是有效的,因为 to_html 只返回一个字符串,我们可以使用字符串对象的 python 替换方法将任何部分替换为其他任何部分(请注意,我只使用了开头的“<”)。我用它来包含样式到<thead>ie。表的标题部分!