'How to format print string to 2 decimal places as well as right aligning each set of values
Once the user selects a colchoice to view the statistics for a given column. I would like to print the output line by line, also to 2 decimal places and get each row to align properly. How can I go about this?
import pandas as pd
import os
data = {'first':[1,2,3,4,5,6,7,8], 'second':[34,23,12,56,76,23,123, 19], 'third':[123,34,2,45,56,76,324,23]}
data = pd.DataFrame(data)
print(data)
def analyse_data(data):
colchoice = input("Please select a column to analyse: ")
while True:
if colchoice in data:
print(f"{colchoice} \n--------\n number of values(n):\t {data[colchoice].count()} \n minimum value:\t {data[colchoice].min()}\n maximum value:\t {data[colchoice].max()}\n mean: \t {data[colchoice].mean()}\n median: \t {data[colchoice].median()}\n standard deviation: \t {data[colchoice].std()} \n std.error. of mean: \t {data[colchoice].sem()}", sep=os.linesep)
break
else:
print("Please try again")
colchoice = input("Please select a column to analyse: ")
analyse_data(data)
Solution 1:[1]
See the format documentation here -- https://docs.python.org/3/library/string.html#format-specification-mini-language
There are examples for aligning and padding lines.
>>> print("This is: {:>10.2f}".format(45.2343))
This is: 45.23
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brendan Abel |
