我是刚入门自学的入门级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] 删除。
我来说两句