Python – 计算Pandas DataFrame中某一列的标准差

Python – 计算Pandas DataFrame中某一列的标准差

要计算标准差,请使用Pandas的std()方法。首先,导入所需的Pandas库-

import pandas as pd
Python

现在,创建一个包含两列的DataFrame-

dataFrame1 = pd.DataFrame(
   {
      "汽车": ['宝马', '雷克萨斯', '奥迪', '特斯拉', '宾利', '捷豹'],
      "销量": [100, 150, 110, 80, 110, 90]
   }
)
Python

使用std()方法找到“销量”列值的标准差-

printDataFrame1中销量列的标准差= ”,dataFrame1['销量'].std()
Python

同样的方式,我们也从第二个DataFrame中计算了标准差。

示例

以下是完整的代码 –

#
# Python - 计算Pandas DataFrame的列值的标准差
#

import pandas as pd

# 创建DataFrame1
dataFrame1 = pd.DataFrame(
   {
      "汽车": ['宝马', '雷克萨斯', '奥迪', '特斯拉', '宾利', '捷豹'],
      "销量": [100, 150, 110, 80, 110, 90]
   }
)

printDataFrame1 ... \ n”,dataFrame1

# 查找“销量”列值的标准差
printDataFrame1中销量列的标准差= ”,dataFrame1['销量'].std()

# 创建DataFrame2
dataFrame2 = pd.DataFrame(
   {
      "产品": ['电视', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],
      "价格": [8000, 500, 3000, 1500, 3000, 4000]
   }
)

print\nDataFrame2 ... \ n”,dataFrame2

# 查找“价格”列值的标准差
printDataFrame2中价格列的标准差= ”,dataFrame2['价格'].std()
Python

输出

这将产生以下输出-

DataFrame1 ...
       汽车   销量
0     宝马    100
1   雷克萨斯    150
2     奥迪    110
3   特斯拉     80
4   宾利    110
5     捷豹     90
DataFrame1中销量列的标准差= 24.2212028328

DataFrame2 ...
    价格   产品
0   8000         电视
1   500    PenDrive
2   3000  HeadPhone
3   1500   EarPhone
4   3000        HDD
5   4000        SSD
DataFrame2中价格列的标准差= 2601.28173535
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册