You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中一次性查看多个DataFrame的.head()结果?

如何同时查看多个DataFrame的头部数据?

你已经通过连续打印三个DataFrame的head()结果实现需求,这里还有几种更灵活的方法:

方法1:循环遍历DataFrame列表

把需要查看的DataFrame放进列表,通过循环批量调用head(),代码更简洁,还能添加分割线区分不同结果:

# 把三个DataFrame存入列表
dfs = [ride_sharing, airlines, banking]

# 循环打印每个DataFrame的头部数据
for df in dfs:
    print(df.head())
    print("="*50)  # 可选:添加分割线,让输出更清晰

方法2:整合为单个DataFrame查看

如果希望把所有头部数据合并到一个表格里,可以给每个数据块添加来源标识后拼接,方便对比:

import pandas as pd

dfs = [ride_sharing, airlines, banking]
df_names = ["ride_sharing", "airlines", "banking"]
combined_heads = []

for name, df in zip(df_names, dfs):
    # 取出头部数据并添加来源列
    head_data = df.head().copy()
    head_data["来源DataFrame"] = name
    combined_heads.append(head_data)

# 拼接所有头部数据
result = pd.concat(combined_heads, axis=0)
print(result)

方法3:交互式环境下的美观展示

如果你在Jupyter Notebook/IPython环境中,使用IPython.display.display可以以美观的表格形式逐个展示每个DataFrame的头部:

from IPython.display import display

dfs = [ride_sharing, airlines, banking]
for df in dfs:
    display(df.head())

内容的提问来源于stack exchange,提问作者Thamy Mitidieri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 13:43:15