Home > Software engineering >  Python List Statistics not correct
Python List Statistics not correct

Time:11-14

I try to calculate some stats for a list. But somehow these are not correct: Code: import pandas as pd

import statistics


list_runs_stats=[4.149432, 3.133142, 3.182976, 2.620959, 3.200038, 2.66668, 2.604444, 2.683382, 3.249564, 3.149947]

list_stats=pd.Series(list_runs_stats).describe()

print (list_stats.mean())
print (list_stats.min())
print (list_stats.max())
print (list_stats.median())
print (list_stats.count())

Result:

3.6617099664905832
0.467574831924664
10.0
3.10280045
8

I think min, max and count is quite obvious that it is not correct. Excel gives me mean: 3.0640564 and median:3,1415445

What I am doing wrong?

CodePudding user response:

By assigning the output of describe() to list_stats, you are calculating the min and max of the output of describe function

Can you try this this instead?

import pandas as pd
list_runs_stats=[4.149432, 3.133142, 3.182976, 2.620959, 3.200038, 2.66668, 2.604444, 2.683382, 3.249564, 3.149947]
df = pd.Series(list_runs_stats)
df.describe()
#Output
count    10.000000
mean      3.064056
std       0.467575
min       2.604444
25%       2.670856
50%       3.141545
75%       3.195773
max       4.149432
dtype: float64

CodePudding user response:

Seems like this post can help you out: Finding the average of a list

for example:

print(statistics.mean(list_runs_stats)) # would print 3.0640564

CodePudding user response:

instead with printing check with print(list_stats) or display in Jupiter. print(list_stats)

count 10.000000
mean 3.064056
std 0.467575
min 2.604444
25% 2.670856
50% 3.141545
75% 3.195773
max 4.149432
dtype: float64

or use dict form for print like
list_stats["min"]

  • Related