Home > OS >  Why do I have to add print function thrice in python i need to give print only once to get the outpu
Why do I have to add print function thrice in python i need to give print only once to get the outpu

Time:12-19

i am trying to find mean, trimmed mean, and median from a dataset with file format .csv in python below is the code. My question is i am unable to get all values at once using print function just once i need to write print function thrice is there anything to shorten my code.

import pandas as pd
from scipy.stats import trim_mean
f = pd.read_csv('E:\pop.csv')
print(trim_mean(f['Population'],0.1))
print(f['Population'].mean())
print(f['Population'].median())

CodePudding user response:

You could do it like this:

import pandas as pd
from scipy.stats import trim_mean

pop = pd.read_csv(r'E:\pop.csv')['Population']
print(f'Trim mean={trim_mean(pop, 0.1)}, Mean={pop.mean()}, Median={pop.median()}')

Note the raw string notation

CodePudding user response:

Just do this

print(trim_mean(f['Population'],0.1), f['Population'].mean()),  f['Population'].median())

Even better, use f-string

print(f”Trim mean: {trim_mean(f['Population'],0.1)},   Mean: {f['Population'].mean()},  median: {f['Population'].median()}”)
  • Related