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
- First, import the required libraries:
import pandas as pd from IPython.display import display
- 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 )
- Display each DataFrame separately to get them stacked vertically:
display(df1) display(df2)
Sample Output
df1 Output:
| State | L.A. | L.A. | L.A. | |
|---|---|---|---|---|
| City | Arcadia | Bell | Durate | |
| 2018-02-22 | 2.05 | 3.07 | 1.96 | |
| 2018-02-25 | 4.82 | 2.56 | 3.14 | |
| 2018-03-01 | 2.51 | 3.45 | 3.02 |
df2 Output:
| State | G.A. | G.A. | ... | |
|---|---|---|---|---|
| City | Athens | Atlanta | ... | |
| [Date Values] | [Data] | [Data] | ... |
Quick Notes
- In Jupyter Notebooks, you can skip importing
displayentirely—just writedf1followed bydf2in the same cell, and they’ll render stacked automatically. - If you’re working in a regular Python script (non-interactive environment), using
print(df1)andprint(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
相关产品推荐
相关产品推荐

