Reputation: 184
I have this Python dictionary as output.
{'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 the date is a dynamic list like.
And I have a static job list.
How could I transform this dict like the below picture?
PS - the captured first element from the date dictionary.
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
Upvotes: 5
Views: 23805
Reputation: 17064
You can do it with pandas like so:
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:
If you want just the first element of the list:
df = df.applymap(lambda x: x[0] if type(x)==list else x)
df
If you want to convert it into HTML table you can use .to_html()
method like so:
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>
Upvotes: 25