【问题标题】:Print Visually Pleasing DataFrames in For Loop in Jupyter Notebook Pandas在 Jupyter Notebook Pandas 的 For 循环中打印视觉上令人愉悦的数据帧
【发布时间】:2018-12-19 16:40:40
【问题描述】:

假设我在一个列表中有这两个数据框,

sm = pd.DataFrame([["Forever", 'BenHarper'],["Steel My Kisses", 'Kack Johnson'],\
                  ["Diamond On the Inside",'Xavier Rudd'],[ "Count On Me", "Bruno Mars"]],\
                   columns=["Song", "Artist"])

pm = pd.DataFrame([["I am yours", 'Jack Johnson'],["Chasing Cars", 'Snow Patrol'],\
                  ["Kingdom Comes",'Cold Play'],[ "Time of your life", "GreenDay"]],\
                   columns=["Song", "Artist"])

df_list = [sm,pm]

现在,当我想在迭代时打印两个数据帧时,我会得到这样的结果,

for i in df_list:
    print(i)

结果,

                  Song        Artist
0                Forever     BenHarper
1        Steel My Kisses  Kack Johnson
2  Diamond On the Inside   Xavier Rudd
3            Count On Me    Bruno Mars
                Song        Artist
0         I am yours  Jack Johnson
1       Chasing Cars   Snow Patrol
2      Kingdom Comes     Cold Play
3  Time of your life      GreenDay

但是,当我们执行df_list[0] 时,它会以令人愉悦的表格方式打印,

当我遍历列表并打印数据框时,我能否以视觉上令人愉悦的方式获得同样的效果?我一直在寻找,还没有运气。任何想法如何做到这一点?

(对不起,如果这在 python 中是正常的,因为我是 Python 和 Jupyter 的新手,视觉上令人愉悦的东西让我很高兴看到)

【问题讨论】:

    标签: python python-3.x dataframe jupyter-notebook


    【解决方案1】:

    你可以用这个:

    from IPython.display import display
    
    for i in df_list:
        display(i)
    

    Jupyter Notebook Viewer了解更多关于丰富和灵活格式的技巧

    【讨论】:

    • 在最新的 Python Jupyter 版本中,不需要from IPython.display import display
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2018-08-25
    • 2015-05-05
    • 2022-01-04
    相关资源
    最近更新 更多