python笔记:4.1.1.1统计量_集中趋势_均值

# -*- coding: utf-8 -*-
"""
Created on Sat May 25 23:11:30 2019

@author: User
"""
import pandas as pd
import numpy as np

stock=np.dtype([('id',np.str,5),
                ('time',np.str,10),
                ('code',np.str,10),
                ('open_p',np.float64),
                ('close_p',np.float64),
                ('low_p',np.float64),
                ('vol',np.int32),
                ('high_p',np.float64),
                ('col',np.int32)])

print('\n np.loadtxt:')
jd_stock=np.loadtxt('data\stock.csv',delimiter=',',dtype=stock)
print(jd_stock)

print('\n pd.read_table:')
jddf=pd.read_csv('data\stock.csv',sep=',',header=None,
                   names=['id','time','code','open_p','colse_p','low_p','vol','high_p','col'])
print(jddf.head())

print("\n np.mean(jd_stock['open_p']):")
print(np.mean(jd_stock['open_p']))

print("\n np.average(jd_stock['open_p']):")
print(np.average(jd_stock['open_p']))

print("\n np.average加权平均 np.average(jd_stock['open_p'], weights=jd_stock['vol']):")
print(np.average(jd_stock['open_p'], weights=jd_stock['vol']))

运行:

 np.loadtxt:
[('1', '20130902', '600028', 4.41, 4.43, 4.37,  17275, 4.41,  392662)
 ('2', '20130903', '600028', 4.41, 4.46, 4.4 ,  19241, 4.45,  434177)
 ('3', '20130904', '600028', 4.44, 4.49, 4.42,  20106, 4.47,  451470) ...
 ('1356', '20190327', '600028', 5.71, 5.75, 5.69,  63601, 5.72, 1112544)
 ('1357', '20190328', '600028', 5.69, 5.7 , 5.62,  65692, 5.64, 1162484)
 ('1358', '20190329', '600028', 5.65, 5.75, 5.61, 112785, 5.74, 1981482)]

 pd.read_table:
   id      time    code  open_p  colse_p  low_p       vol  high_p     col
0   1  20130902  600028    4.41     4.43   4.37  17275.39    4.41  392662
1   2  20130903  600028    4.41     4.46   4.40  19241.84    4.45  434177
2   3  20130904  600028    4.44     4.49   4.42  20106.30    4.47  451470
3   4  20130905  600028    4.47     4.48   4.42  15582.48    4.47  349997
4   5  20130906  600028    4.46     4.52   4.45  19101.41    4.50  425777

 np.mean(jd_stock['open_p']):
5.658718703976436

 np.average(jd_stock['open_p']):
5.658718703976436

 np.average加权平均 np.average(jd_stock['open_p'], weights=jd_stock['vol']):
6.362912722690805
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值