How to calculate 1st and 3rd quartiles?

前端 未结 10 1169
眼角桃花
眼角桃花 2020-12-04 19:14

I have DataFrame:

    time_diff   avg_trips
0   0.450000    1.0
1   0.483333    1.0
2   0.500000    1.0
3   0.516667    1.0
4   0.533333    2.0
10条回答
  •  旧时难觅i
    2020-12-04 19:57

    If you want to use raw python rather than numpy or panda, you can use the python stats module to find the median of the upper and lower half of the list:

        >>> import statistics as stat
        >>> def quartile(data):
                data.sort()               
                half_list = int(len(data)//2)
                upper_quartile = stat.median(data[-half_list]
                lower_quartile = stat.median(data[:half_list])
                print("Lower Quartile: "+str(lower_quartile))
                print("Upper Quartile: "+str(upper_quartile))
                print("Interquartile Range: "+str(upper_quartile-lower_quartile)
    
        >>> quartile(df.time_diff)
    

    Line 1: import the statistics module under the alias "stat"

    Line 2: define the quartile function

    Line 3: sort the data into ascending order

    Line 4: get the length of half of the list

    Line 5: get the median of the lower half of the list

    Line 6: get the median of the upper half of the list

    Line 7: print the lower quartile

    Line 8: print the upper quartile

    Line 9: print the interquartile range

    Line 10: run the quartile function for the time_diff column of the DataFrame

提交回复
热议问题