I am trying to print a pandas frame which has a two-level column index. I issue the following command in IPYthon
print(WEO_diffs)
The frame has column indexes as follows
In [862]: WEO_diffs.columnsOut[862]:MultiIndex([('Oct2023-Jul2023', 2023), ('Oct2023-Jul2023', 2024), ('Oct2023-Jul2023', 2025), ('Oct2023-Apr2023', 2023), ('Oct2023-Apr2023', 2024), ('Oct2023-Apr2023', 2025), ('Jul2023-Apr2023', 2023), ('Jul2023-Apr2023', 2024), ('Jul2023-Apr2023', 2025)], names=['WEO', 'year'])
And when I print the frame, I get the following messed up output -- I would like the top level column indexes, e.g. Jul2023-Apr2023 to be centered on top the year data, but they seem to drift and do not even align with the beginning of the lower level index in the same group.
I'm losing my mind over this!
I've tried to print this using tabulate, went through all possible tablefmt combinations; I tried to export the frame as a string with to_string()
, but that did nothing. I don't want to go through Excel or Latex to make this pretty, because the elegance of this is that I want to see a quick report on the screen.
Below is the DF in a copy-pasteable format
Country | ('Oct2023-Jul2023', 2023) | ('Oct2023-Jul2023', 2024) | ('Oct2023-Jul2023', 2025) | ('Oct2023-Apr2023', 2023) | ('Oct2023-Apr2023', 2024) | ('Oct2023-Apr2023', 2025) | ('Jul2023-Apr2023', 2023) | ('Jul2023-Apr2023', 2024) | ('Jul2023-Apr2023', 2025) |
---|---|---|---|---|---|---|---|---|---|
Advanced Economies | 0.0 | 0.1 | -0.0 | 0.3 | 0.1 | 0.0 | 0.2 | 0.0 | 0.0 |
Brazil | 1.0 | 0.3 | 0.2 | 2.2 | 0.0 | -0.0 | 1.1 | -0.3 | -0.2 |
Canada | -0.4 | 0.2 | 0.1 | -0.2 | 0.1 | 0.1 | 0.2 | -0.1 | -0.0 |
China | -0.2 | -0.3 | 0.0 | -0.2 | -0.3 | 0.0 | -0.0 | 0.0 | -0.0 |
Euro area | -0.3 | -0.3 | -0.0 | -0.1 | -0.1 | -0.1 | 0.1 | 0.1 | -0.0 |
France | 0.2 | 0.0 | -0.1 | 0.3 | 0.1 | -0.1 | 0.1 | 0.1 | 0.0 |
Germany | -0.2 | -0.4 | 0.1 | -0.4 | -0.2 | -0.1 | -0.2 | 0.2 | -0.1 |
India | 0.2 | -0.0 | 0.1 | 0.4 | -0.0 | 0.1 | 0.2 | -0.0 | -0.0 |
Italy | -0.5 | -0.2 | -0.1 | 0.0 | -0.1 | -0.2 | 0.5 | 0.1 | -0.1 |
Japan | 0.6 | 0.0 | 0.1 | 0.7 | 0.0 | 0.1 | 0.1 | -0.0 | -0.0 |
Mexico | 0.5 | 0.7 | -0.6 | 1.4 | 0.6 | -0.4 | 0.8 | -0.1 | 0.2 |
Russia | 0.7 | -0.2 | 0.1 | 1.5 | -0.2 | -0.0 | 0.8 | 0.0 | -0.2 |
Saudi Arabia | -1.1 | 1.2 | 0.0 | -2.3 | 0.8 | 1.3 | -1.1 | -0.3 | 1.3 |
South Africa | 0.6 | 0.1 | 0.0 | 0.8 | -0.0 | 0.0 | 0.2 | -0.1 | 0.0 |
Spain | -0.0 | -0.3 | 0.0 | 0.9 | -0.3 | 0.0 | 1.0 | -0.0 | 0.0 |
United Kingdom | 0.1 | -0.4 | -0.2 | 0.7 | -0.3 | -0.2 | 0.6 | 0.1 | -0.0 |
United States | 0.3 | 0.4 | 0.0 | 0.5 | 0.4 | 0.1 | 0.3 | -0.0 | 0.0 |
World | 0.0 | -0.0 | 0.0 | 0.1 | -0.1 | 0.0 | 0.1 | -0.0 | 0.0 |
Any help would be **greatly **appreciated!Thanks in advance.