美文网首页
Python-科学计算-pandas-04-统计数据

Python-科学计算-pandas-04-统计数据

作者: Data_Python_VBA | 来源:发表于2019-11-16 12:03 被阅读0次

微信公众号原文

系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2

  • 这个系列讲讲Python的科学计算版块
  • 今天讲讲pandas模块:获取某列的一些统计结果,包括最大/最小值/均值/标准方差等

Part 1:示例

  1. 已知一个DataFrame,有4列["quality_1", "measure_value", "up_tol", "down_tol"]
  2. 获取测量值列的一些统计数据

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)

代码截图

1.png

执行结果

2.png

Part 3:部分代码解读

  1. statistic_value = df.describe(),对数值列进行统计计算,输出结果分类:
    • 样本数目
    • 均值
    • 标准方差
    • 最小值
    • 25%位数
    • 50%位数,即中位数
    • 75%位数
    • 最大值
  2. df[["measure_value"]] = df[["measure_value"]].astype(float),对measure_value列进行数据类型转换

本文为原创作品,欢迎分享朋友圈

长按图片识别二维码,关注本公众号
Python 优雅 帅气


12x0.8.jpg

相关文章

网友评论

      本文标题:Python-科学计算-pandas-04-统计数据

      本文链接:https://www.haomeiwen.com/subject/mhcyictx.html