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

Python Pandas:如何将独立DataFrame上下放置于电子表格中

Displaying Multiple Independent DataFrames Vertically

If you want to show multiple pandas DataFrames stacked vertically (one below the other) while keeping them completely separate (no merging or concatenation), the easiest way is to display each DataFrame individually. This works seamlessly in Jupyter Notebooks or IPython environments, where each table will render with proper formatting.

Step-by-Step Implementation

  1. First, import the required libraries:
import pandas as pd
from IPython.display import display
  1. Create your pivot tables as you outlined:
# Assume data1, data2, and metric are already defined in your code
df1 = data1.pivot_table(
    index=pd.to_datetime(data1['Date']),
    columns=['State', 'City'],
    values=metric
)

df2 = data2.pivot_table(
    index=pd.to_datetime(data2['Date']),
    columns=['State', 'City'],
    values=metric
)
  1. Display each DataFrame separately to get them stacked vertically:
display(df1)
display(df2)

Sample Output

df1 Output:

StateL.A.L.A.L.A.
CityArcadiaBellDurate
2018-02-222.053.071.96
2018-02-254.822.563.14
2018-03-012.513.453.02

df2 Output:

StateG.A.G.A....
CityAthensAtlanta...
[Date Values][Data][Data]...

Quick Notes

  • In Jupyter Notebooks, you can skip importing display entirely—just write df1 followed by df2 in the same cell, and they’ll render stacked automatically.
  • If you’re working in a regular Python script (non-interactive environment), using print(df1) and print(df2) will also show the tables one after the other, though the formatting won’t be as clean as in notebooks.

内容的提问来源于stack exchange,提问作者Michael Isaza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:32:02