python之pandas的应用第八天

本文介绍了Pandas库在数据分析中的应用,包括使用Series计算连续三天的和,通过pandas_datareader获取百度股票数据并计算十日均线,绘制股价折线图,展示了范围索引的创建以及多级索引在数据组织中的运用。此外,还涉及了时间序列的等分和获取星期天的日期。
摘要由CSDN通过智能技术生成

pandas的应用第八天

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

%config InlineBackend.figure_format = 'svg'

ser1 = pd.Series(np.random.randint(10, 100, 10))
# 连续三天的和
ser1.rolling(3).sum()


import pandas_datareader as pdr

baidu_df = pdr.get_data_stooq('BIDU', start='2021-11-1', end='2021-12-8')
baidu_df.sort_index(inplace=True)
# 十日均线
baidu_df.Close.rolling(10).mean()

# 绘制折线图
plt.plot(np.arange(baidu_df.index.size), baidu_df.Close)
plt.xticks(np.arange(baidu_df.index.size),
           rotation=45,
           labels=baidu_df.index.month.astype(str).values + '-' + baidu_df.index.day.astype(str).values)
plt.yticks(np.arange(100, 181, 10))
plt.show()

# 范围索引
sales_data = np.random.randint(400, 1000, 12)
month_index = pd.RangeIndex(1, 13, name='月份')
ser = pd.Series(data=sales_data, index=month_index)


# 多级索引
ids = np.arange(1001, 1006)
sms = ['期中', '期末']
index = pd.MultiIndex.from_product((ids, sms), names=['学号', '学期'])
courses = ['语文', '数学', '英语']
scores = np.random.randint(60, 101, (10, 3))
df = pd.DataFrame(data=scores, columns=courses, index=index)

# 将时间平均分为多少份
pd.date_range('2021-1-1', '2021-6-1', periods=10)

# 返回日期中的星期天
temp = pd.date_range('2021-1-1', '2021-6-1', freq='W')
temp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

azured_xu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值