【发布时间】:2020-10-20 15:16:54
【问题描述】:
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
【问题讨论】:
标签: python pandas statistics return median