iPython Notebook not printing Dataframe as table

守給你的承諾、 提交于 2019-12-24 04:59:27

问题


I'm trying to print a df in ipython notebook but it doesn't print it as a table.

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
        'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
        'wins': [11, 8, 10, 15, 11, 6, 10, 4],
        'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses'])

print football

Output

   year     team  wins  losses
0  2010    Bears    11       5
1  2011    Bears     8       8
2  2012    Bears    10       6
3  2011  Packers    15       1
4  2012  Packers    11       5
5  2010    Lions     6      10
6  2011    Lions    10       6
7  2012    Lions     4      12

I tried "display" as suggested Show DataFrame as table in iPython Notebook:

from IPython.display import display
#.....
print display(football)

Also tried:

pandas.core.format.set_printoptions(notebook_repr_html=True)

but got error:

AttributeError: 'module' object has no attribute 'set_printoptions'

How can I print my df as a table with nice vertical and horizontal lines?


回答1:


set_printoptions has been replaced by set_options in the more recent versions of pandas, try to use:

pandas.set_option('display.notebook_repr_html', True)

Also do not use print, simply state football or display(football) in the last line of a notebook cell.



来源:https://stackoverflow.com/questions/27801379/ipython-notebook-not-printing-dataframe-as-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!