Jupyter笔记本不使用制表打印表

时间:2018-03-19 23:58:39

标签: python jupyter-notebook

我有一个在我的Unix机器上运行完美的python脚本。在这个脚本中,我使用制表来打印几个表。但是,当我试图用它来制作一个Jupyter笔记本时,它执行得很好,但却与表格输出相混淆。 我尝试了几种不同的方法,例如使用不同的表格格式,例如HTML' HTML'但在任何地方都无法找到任何帮助。 请帮忙。 这是我想在Jupyter笔记本中使用相同的python代码的一部分。 对于数据文件 - purchase_data.json,请检查my github repo

from tabulate import tabulate
import json
import pandas as pd
import numpy as np

with open('purchase_data.json') as json_data:
        data = json.load(json_data)
        df = pd.DataFrame(data)

print (" Heroes of Pymoli","\n")
print ("==================","\n")

# Total Number of Players
playersCount = df.groupby('SN')['Item ID'].nunique().count()
print ("**Total Number of Players**", "\n")
print (tabulate([[playersCount]], headers=['Total Players'], tablefmt='fancy_grid').encode('utf-8'))

#*Purchasing Analysis (Gender)** 
# * Purchase Count
# * Average Purchase Price
# * Total Purchase Value
# * normalized totals
genderPur = df.groupby('Gender').agg({'Price':['count','mean','sum']})
print ("\n**Analysis (Gender)**","\n")
print (tabulate(genderPur,
        headers=['Purchase Count', 'Average Purchase Price', 'Total Purchase Value'], 
        tablefmt='fancy_grid').encode('utf-8'),"\n")

1 个答案:

答案 0 :(得分:0)

我后来才明白这一点,所以我认为我的回答也可能对其他人有所帮助。在我的Jupyter笔记本中,当Tabulate不起作用时,显示命令有效。 例如: -

import json
import pandas as pd
import numpy as np
from IPython.display import display

with open('purchase_data.json') as json_data:
    data = json.load(json_data)
    df = pd.DataFrame(data)

print (" Heroes of Pymoli","\n")
# Total Number of Players
playersCount = df.groupby('SN')['Item ID'].nunique().count()
print ("**Total Number of Players**")
display (playersCount)

对于整个代码,请点击this link