Python-科学计算-pandas-04-统计数据
2019-11-16 本文已影响0人
Data_Python_VBA
系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
- 这个系列讲讲Python的科学计算版块
- 今天讲讲pandas模块:获取某列的一些统计结果,包括最大/最小值/均值/标准方差等
Part 1:示例
- 已知一个
DataFrame
,有4列["quality_1", "measure_value", "up_tol", "down_tol"]
- 获取测量值列的一些统计数据
Part 2:代码
import pandas as pd
dict_1 = {"quality_1": ["pos_1", "pos_2", "pos_3", "pos_4", "pos_5"],
"measure_value": ["6", "4", 6, 3.5, 2.5],
"up_tol": [5, 5, 3, 3, 2],
"down_tol": [-5, -5, -3, -3, 2]}
df = pd.DataFrame(dict_1, columns=["quality_1", "measure_value", "up_tol", "down_tol"])
print(df)
statistic_value = df.describe()
print(statistic_value)
print("\n", "数据类型转换后")
df[["measure_value"]] = df[["measure_value"]].astype(float)
print(df)
statistic_value = df.describe()
print(statistic_value)
代码截图

执行结果

Part 3:部分代码解读
-
statistic_value = df.describe()
,对数值列进行统计计算,输出结果分类:- 样本数目
- 均值
- 标准方差
- 最小值
- 25%位数
- 50%位数,即中位数
- 75%位数
- 最大值
-
df[["measure_value"]] = df[["measure_value"]].astype(float)
,对measure_value
列进行数据类型转换
本文为原创作品,欢迎分享朋友圈
长按图片识别二维码,关注本公众号
Python 优雅 帅气
