对于分类数据,如何在表而不是列表中显示结果?(Jupyter笔记本/ Python)

多云绿色

我是刚入门自学的入门级Python用户,使用Python进行数据分析。这些天,我正在使用Kaggle上的Jupyter Notebook中的“全球自杀率”数据进行练习。我在格式化结果时遇到了一些问题。我想知道如何将我的结果放在几个列表中,变成格式正确的表格?

我使用的数据集是全球自杀日期数据。对于代码的以下部分,我想检索min_year(即1985)和max_year(即2016)中的所有国家/地区信息。

所以我期望的输出是这样的:(只是一个例子)

预期产量

以下是我的代码。

country_1985 = data [(data['year']==min_year)].country.unique()
country_2016 = data [(data['year']==max_year)].country.unique()

print ([country_1985],[country_2016])

结果显示如下:

结果

但是,我不要那些在列表中。我希望它以表格格式显示,如下所示结果

我尝试使用pandas.DataFrame,也没有任何意义...有人可以帮助我解决我的问题吗?

更新:

感谢@Code Pope代码!!!感谢您的解释和耐心等待!

import pandas as pd
import numpy as np


country_1985 = data [(data['year']==min_year)].country.unique()
country_2016 = data [(data['year']==max_year)].country.unique()

country_1985 = pd.DataFrame(country_1985.categories)
country_2016 = pd.DataFrame(country_2016.categories)

# Following are the code from @Code Pope

from IPython.display import display_html
def display_side_by_side(dataframe1, dataframe2):
modified_HTML=dataframe1.to_html() + dataframe2.to_html()
display_html(modified_HTML.replace('table','table 
style="display:inline"'),raw=True)

display_side_by_side(country_1985,country_2016 )

然后看起来像这样:更新的输出

教皇教区

就像您说的是使用Jupyter Notebook一样,您可以在显示数据框之前更改它的html。使用以下功能:

from IPython.display import display_html
def display_side_by_side(dataframe1, dataframe2):
    modified_HTML=dataframe1.to_html() + dataframe2.to_html()
    display_html(modified_HTML.replace('table','table style="display:inline"'),raw=True)

# then call the function with your two dataframes
display_side_by_side(country_1985,country_2016 )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在jupyter笔记本python中密谋

如何每天自动运行python jupyter笔记本

如何在Jupyter笔记本中运行Python异步代码?

如何在jupyter笔记本中使用python向图像添加视觉注释?

在jupyter笔记本中创建原始python文件

如何在python中获取当前的jupyter笔记本服务器?

在Jupyter Python笔记本中清除MatPlotLib图

如何在Python / Jupyter笔记本中省略matplotlib打印输出?

如何在我的jupyter笔记本中添加python3内核?

Jupyter可以在Python笔记本中运行单独的R笔记本吗?

如何在每个jupyter笔记本内核之前运行Python代码

如何从离线的可打印python jupyter笔记本中的选定点获取数据?

如何在jupyter笔记本上将python脚本另存为.py文件

如何在Jupyter笔记本中的相同位置更新python循环中的matpplotlib lib图?

如何!rm python_var(在Jupyter笔记本中)

一旦满足条件,如何停止python程序(在jupyter笔记本中)

如何在jupyter笔记本中隐藏单元格输出(VSCode + Python扩展)?

如何在PyCharm中将Markdown嵌入用作jupyter笔记本的python文件中

如何根据python中pandas数据框中的列进行分组并按降序排列?(Jupyter笔记本)

在python jupyter笔记本中将列表转换为数据框

如何在Google Cloud Datalab的另一个笔记本中执行python笔记本

Python和R:如何在Jupyter笔记本中使用Pyper显示图?

如何使用REST API运行齐柏林飞艇笔记本并在python中返回结果?

在 Jupyter 笔记本中使用 Python 从 github 导入数据

Python 中的缩进错误(在 Jupyter 笔记本中)

如何从 Azure 数据块获取 python 笔记本路径?

如何在jupyter笔记本中的同一环境中在python包的本地副本和基本副本之间切换以进行开发

Python Jupyter 笔记本 scipy

VSCODE JUPYTER - 如何在笔记本 (.ipynb) 中显示与 VSCode 中的 Python (.py) 相同的自动完成?