首页 > 解决方案 > 为什么统计模块只返回列名,而不返回中值?

问题描述

酒精文件

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

标签: pythonpandasstatisticsreturnmedian

解决方案


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

推荐阅读