python正则表达式详解 pandas_python学习-pandas系列

# 首先 准备生成一个关于基金数据的Series

def get_fund_series():

import requests

from dateutil import parser

import datetime

headers = {

'Connection': 'keep-alive',

'Cache-Control': 'max-age=0',

'Upgrade-Insecure-Requests': '1',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',

'Sec-Fetch-User': '?1',

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',

'Sec-Fetch-Site': 'none',

'Sec-Fetch-Mode': 'navigate',

'Accept-Encoding': 'gzip, deflate, br',

'Accept-Language': 'zh-CN,zh;q=0.9',

}

response = requests.get('https://glink.genius.com.cn/base/V_JRJ_FUND_NET_HISTORY/full=2&sort=TRADEDATE%20desc&filter-FUND_CODE-str=002001&limit=200', headers=headers)

res = response.json()

data = []

index= []

for item in res['rows']:

temp = parser.parse(item['MTIME'])+ datetime.timedelta(hours=8)

temp = temp.strftime('%Y-%m-%d')

if temp not in index:

index.append(temp)

data.append(item['UNIT_NET'])

return index, data

index, data = get_fund_series()

fund = pd.Series(data, index)

# 查看 Series 头部和尾部数据

print(fund.head())

print(fund.tail(3))

2019-12-18 1.512

2019-12-17 1.503

2019-12-14 1.514

2019-12-13 1.496

2019-12-12 1.498

dtype: float64

2019-03-02 1.370

2019-03-01 1.350

2019-02-28 1.343

dtype: float64

# 查看index

print(fund.index)

Index(['2019-12-18', '2019-12-17', '2019-12-14', '2019-12-13', '2019-12-12',

'2019-12-11', '2019-12-10', '2019-12-07', '2019-12-06', '2019-12-05',

...

'2019-03-13', '2019-03-12', '2019-03-09', '2019-03-08', '2019-03-07',

'2019-03-06', '2019-03-05', '2019-03-02', '2019-03-01', '2019-02-28'],

dtype='object', length=198)

# 无论是头部尾部,还是index 我们得到的都是Series类型的数据,也可以转为list/numpy.array/

print(fund.head().values, fund.head().to_numpy(), np.asarray(fund.head()), list(fund.head()))

print('****************')

print(fund.head().array)

[1.512 1.503 1.514 1.496 1.498] [1.512 1.503 1.514 1.496 1.498] [1.512 1.503 1.514 1.496 1.498] [1.512, 1.503, 1.514, 1.496, 1.498]

****************

[1.512, 1.503, 1.514, 1.496, 1.498]

Length: 5, dtype: float64

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值