为什么统计模块只返回列名,不返回中值?

Why the statistics module only returning the name of the column, but not the median value?

alcohol file

import pandas as pd
import statistics as st

def median_1(table):
    print(table.median())
    
def median_2(table):
    print(st.median(table))

# Reading the excel file and sorting the value according to the X column
file=pd.read_excel("C:\Users\hp\Desktop\alcohol.xls").sort_values("X")

#Forming the new index using list comprehension
index_row=[i+1 for i in range(len(file))]

#making the new index compatible
index_new=pd.Index(index_row)

#Extracting the column named X and converting it into dataframe
column_df=pd.DataFrame(file.loc[:,"X"])

#setting the new index 
new=column_df.set_index(index_new)


median_1(new)
median_2(new)

Median_1 正在返回列名和中值,但它应该只返回中值。

median_2 函数不返回中值,它只是返回列的名称。

Output:
runfile('C:/Users/hp/Desktop/eg.py', wdir='C:/Users/hp/Desktop')
X    562.5
dtype: float64
X

st.median() 将列表而不是数据框作为输入。由于 new 是一个数据框,所以它不起作用。您可以在传递参数时指定列。

median_2(new['X']) 
# this will give you the median value without the column name
562.5

df.median() 同样适​​用于 median_1 函数。

median_1(new['X'])
# this will also give you the median value without the column name
562.5