Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. [OrderedDict([('Name', 'Soytra'), ('Class', 'First'), ('Number', '23768'), ('Place', 'NY'), ('Place1', 'LA'), ('Grade', 16.6038)]), OrderedDict([('Name', 'Reuhyta'), ('Class', 'First'), ('Number', '23769'), ('Place', 'NY'), ('Place1', 'LA'), ('Grade', 23.56789)])]
  2.  
  3. In [4]: l = [OrderedDict([('Name', 'Soytra'), ('Class', 'First'), ('Number', '23768'), ('Place', 'NY'), ('Place1', 'LA'
  4. ...: ), ('Grade', 16.6038)]), OrderedDict([('Name', 'Reuhyta'), ('Class', 'First'), ('Number', '23769'), ('Place', '
  5. ...: NY'), ('Place1', 'LA'), ('Grade', 23.56789)])]
  6.  
  7. In [13]: df = pd.DataFrame(l)
  8.  
  9. In [14]: df
  10. Out[14]:
  11. Name Class Number Place Place1 Grade
  12. 0 Soytra First 23768 NY LA 16.60380
  13. 1 Reuhyta First 23769 NY LA 23.56789
  14.  
  15. In [15]: df.to_html()
  16. Out[15]: '<table border="1" class="dataframe">n <thead>n <tr style="text-align: right;">n <th></th>n <th>Name</th>n <th>Class</th>n <th>Number</th>n <th>Place</th>n <th>Place1</th>n <th>Grade</th>n </tr>n </thead>n <tbody>n <tr>n <th>0</th>n <td>Soytra</td>n <td>First</td>n <td>23768</td>n <td>NY</td>n <td>LA</td>n <td>16.60380</td>n </tr>n <tr>n <th>1</th>n <td>Reuhyta</td>n <td>First</td>n <td>23769</td>n <td>NY</td>n <td>LA</td>n <td>23.56789</td>n </tr>n </tbody>n</table>'
  17.  
  18. # convert the ordered dicts into a python list
  19. keys, rows = [], []
  20. for sub_dict in ordered_items:
  21. row = []
  22. for key in sub_dict:
  23. if key not in keys:
  24. keys.append(key)
  25. row.append(sub_dict[key])
  26. rows.append(row)
  27.  
  28. # convert the python list into a HTML table
  29. thead = "<thead>n {}</thead>".format("".join(map(lambda key: "<th>{}</th>".format(key), keys)))
  30. tbody = "<tbody>"
  31. for row in rows:
  32. tbody += "n <tr>{}</tr>".format("".join(map(lambda value: "<td>{}</td>".format(value), row)))
  33. tbody += "</tbody>"
  34. print("<table>" + thead + tbody + "</table>")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement