简体   繁体   中英

Proper indentation of table output in Python

I am using a basic python editor (Wing 101). When printing out tables (for analysis), I get rows and columns all messed up. Please see below:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import os
import matplotlib.pyplot as plt         


#STEP 2: Access the Data files
data = pd.read_csv('data.csv', index_col=0)
data.sort_values(['Year', "Happiness Score"], ascending=[True, False], inplace=True)
#diplay first 10 rows
data.head(10)

And I get this garbage:

[evaluate World Happness Data Analysis.py]
data.head(10)
         Country                     Region  ...   Dystopia Residual  Year
141  Switzerland             Western Europe  ...             2.51738  2015
60       Iceland             Western Europe  ...             2.70201  2015
38       Denmark             Western Europe  ...             2.49204  2015
108       Norway             Western Europe  ...             2.46531  2015
25        Canada              North America  ...             2.45176  2015
46       Finland             Western Europe  ...             2.61955  2015
102  Netherlands             Western Europe  ...             2.46570  2015
140       Sweden             Western Europe  ...             2.37119  2015
103  New Zealand  Australia and New Zealand  ...             2.26425  2015
6      Australia  Australia and New Zealand  ...             2.26646  2015

[10 rows x 12 columns]

Actually, it looks reasonable above. But in Python shell, it is totally messed up. here's the actual output

My question: How can I get the table printout with cell borders?

Change the font settings of your Python shell. Use some monospace font. Most common selections is Courier New or Courier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM