python - 在 Jupyter Notebook Pandas 的 For 循环中打印视觉上令人愉悦的数据帧

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

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

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] 时,它会以令人愉悦的表格方式打印,

enter image description here

当我遍历列表并打印 Data Frame 时,我能否以一种视觉上令人愉悦的方式获得同样的效果?我一直在寻找,但还没有运气。有什么想法吗?

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

最佳答案

你可以使用这个:

from IPython.display import display

for i in df_list:
    display(i)

enter image description here

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

关于python - 在 Jupyter Notebook Pandas 的 For 循环中打印视觉上令人愉悦的数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288869/

相关文章:

python - 从另一个文件中的一个文件访问类实例?

python - 在 Python 中读取 CSV 文件并将数据传递给 Jinja 模板

python - 如何从html页面中提取文本?

python - 从 python 日期时间中提取星期并获取序列号?

R - 获取每组的前 N-1 行

python-3.x - Pandas 使用跨行条件计算每月出现的次数

python - 为什么 setup.py 表面上成功安装了我的包,但实际上却未能安装依赖项?

python - 我的 CSS 和图像没有显示 Django

python - Kivy TextInput后台问题

Python:当所需的索引系列有重复项时旋转 pandas DataFrame