Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14011

Printing pandas frame -- headers misaligned

$
0
0

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.

enter image description here

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 Economies0.00.1-0.00.30.10.00.20.00.0
Brazil1.00.30.22.20.0-0.01.1-0.3-0.2
Canada-0.40.20.1-0.20.10.10.2-0.1-0.0
China-0.2-0.30.0-0.2-0.30.0-0.00.0-0.0
Euro area-0.3-0.3-0.0-0.1-0.1-0.10.10.1-0.0
France0.20.0-0.10.30.1-0.10.10.10.0
Germany-0.2-0.40.1-0.4-0.2-0.1-0.20.2-0.1
India0.2-0.00.10.4-0.00.10.2-0.0-0.0
Italy-0.5-0.2-0.10.0-0.1-0.20.50.1-0.1
Japan0.60.00.10.70.00.10.1-0.0-0.0
Mexico0.50.7-0.61.40.6-0.40.8-0.10.2
Russia0.7-0.20.11.5-0.2-0.00.80.0-0.2
Saudi Arabia-1.11.20.0-2.30.81.3-1.1-0.31.3
South Africa0.60.10.00.8-0.00.00.2-0.10.0
Spain-0.0-0.30.00.9-0.30.01.0-0.00.0
United Kingdom0.1-0.4-0.20.7-0.3-0.20.60.1-0.0
United States0.30.40.00.50.40.10.3-0.00.0
World0.0-0.00.00.1-0.10.00.1-0.00.0

Any help would be **greatly **appreciated!Thanks in advance.


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>