python - 为什么统计模块只返回列名,而不返回中值?
问题描述
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
推荐阅读
- html - 如何在 Rails 的 HTML 文件的提交按钮中添加搜索符号?
- python - 使用 discord.py 时,Tkinter 不想工作
- hadoop - beeline hive jdbc 连接 - 用户:不允许用户模拟 hiveuser
- c++ - Boost 1.64 单元测试编译失败
- css - 我希望图像一次出现一个并保持到最后所有图像都出现。然后它应该循环播放
- python-3.x - 我想从用户那里获取半径值并计算python中的圆面积
- ios - Detect if ViewController presented as FormSheet
- python - 编写 MD5 (SALTED) 密码破解程序
- php - Laravel:将动态表名传递给模型,例如受保护的 $table = "$tablename"
- python - 在 PyCharm 中使用控制台中的命令行运行 python 程序