pandas 如何将 Python 字典转换为 html 表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41671436/
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-14 02:47:06  来源:igfitidea点击:

How convert Python dictionary to html table?

pythonhtmlpandasdictionary

提问by Lakshmi narayana

I have this python dictionary as output.

我有这个 python 字典作为输出。

{'Job1': {'2017-01-10': [44, 33, 11, 75, 22]}, 
'Job 2': {'2017-01-05': [25, 25, 0, 100, 25], '2017-01-10': [50, 50, 0, 100, 25]}, 
'Job 3': {'2017-01-03': [44, 22, 22, 50, 22], '2017-01-04': [66, 36, 30, 54, 22], '2017-01-06': [88, 52, 36, 59, 22], '2017-01-10': [132, 68, 64, 51, 22], '2017-01-02': [22, 9, 13, 40, 22], '2017-01-08': [110, 52, 58, 47, 22]},
 'Job4': {'2017-01-10': [25, 25, 0, 100, 25]}}

where date is dynamic list like and I have static job list.

其中日期是动态列表,我有静态工作列表。

how could I transform this dict like below picture. PS- captured first element from the date dictionary.

我怎么能像下图那样转换这个字典。PS-从日期字典中捕获的第一个元素。

    1/2/2017 1/3/2017   1/4/2017    1/5/2017    1/6/2017    1/7/2017    1/8/2017    1/9/2017    1/10/2017
Job 1                                                                                             44
Job 2                                 25                                                          50
Job 3   22         44          66         88         110        132
Job 4                                                                                                 25

回答by MYGz

You can do it with pandas like so:

你可以像这样用Pandas来做到这一点:

import pandas as pd

a = {'Job1': {'2017-01-10': [44, 33, 11, 75, 22]}, 
'Job2': {'2017-01-05': [25, 25, 0, 100, 25], '2017-01-10': [50, 50, 0, 100, 25]}, 
'Job3': {'2017-01-03': [44, 22, 22, 50, 22], '2017-01-04': [66, 36, 30, 54, 22], '2017-01-06': [88, 52, 36, 59, 22], '2017-01-10': [132, 68, 64, 51, 22], '2017-01-02': [22, 9, 13, 40, 22], '2017-01-08': [110, 52, 58, 47, 22]},
 'Job4': {'2017-01-10': [25, 25, 0, 100, 25]}}
df = pd.DataFrame(data=a)
df = df.fillna(' ').T
df

Output:

输出:

enter image description here

在此处输入图片说明

If you want just the first element of the list:

如果您只想要列表的第一个元素:

df = df.applymap(lambda x: x[0] if type(x)==list else x)
df

enter image description here

在此处输入图片说明

If you want to convert it into HTML table you can use .to_html()method like so:

如果要将其转换为 HTML 表格,可以使用如下.to_html()方法:

print df.to_html()

Output:

输出:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>2017-01-02</th>
      <th>2017-01-03</th>
      <th>2017-01-04</th>
      <th>2017-01-05</th>
      <th>2017-01-06</th>
      <th>2017-01-08</th>
      <th>2017-01-10</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Job1</th>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td>44</td>
    </tr>
    <tr>
      <th>Job2</th>
      <td></td>
      <td></td>
      <td></td>
      <td>25</td>
      <td></td>
      <td></td>
      <td>50</td>
    </tr>
    <tr>
      <th>Job3</th>
      <td>22</td>
      <td>44</td>
      <td>66</td>
      <td></td>
      <td>88</td>
      <td>110</td>
      <td>132</td>
    </tr>
    <tr>
      <th>Job4</th>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td>25</td>
    </tr>
  </tbody>
</table>