数据分析项目:股票数据预处理/双均线量化策略/各州人口分布分析/美国大选献金项目数据分析

1,股票数据预处理(计算茅台酒股票从15年到21年买卖的收入)
import tushare as ts
import pandas as pd
from pandas import DataFrame,Series
import numpy as np
#获取某只股票的历史行情数据
#code股票代码,data交易时间,open开盘价,close收盘价,volume成交量
df=ts.get_k_data(code=‘600519’,start=‘2000-01-01’)
#将互联网上获取的数据存到本地,to_xxx为写入本地
df.to_csv(’./maotao.csv’)
#将本地的文本数据读到df中
df=pd.read_csv(’./maotao.csv’)
#查看每一列的数据类型
#df.info()
#将time列转为时间序列类型
df[‘date’]=pd.to_datetime(df[‘date’])
#删除df中Unnamed:0的一列,axis=0表示列,1表示行,在drop中相反
df.drop(labels=‘Unnamed: 0’,axis=1,inplace=True)
#将date列作为源数据的行索引
df.set_index(‘date’,inplace=True)
#df.loc[(df[‘open’]-df[‘close’])/df[‘open’]>0.03].index
#df[‘close’].shift(1)
new_df=df[‘2015-01’:‘2021-2’]
#每月第一个交易日对应的行数据
df_monthly=new_df.resample(‘M’).first()
#买入金额总花费
cost=df_monthly[‘open’].sum()100
df_yearly=new_df.resample(‘A’).last()[:-1]
#计算卖出股票到手的钱
resv=df_yearly[‘open’].sum()1200
#最后手中剩余的股票需要估量其价值总收益中
#取2020年12月最后收盘的数据,两个月没收盘故×200
last_money=new_df[‘close’][-1]200
resv+last_money-cost
2,双均线量化策略
import tushare as ts
import pandas as pd
from pandas import Series
from pandas import DataFrame,Series
import numpy as np
import matplotlib.pyplot as plt
#获取某只股票的历史行情数据
#code股票代码,data交易时间,open开盘价,close收盘价,volume成交量
df=ts.get_k_data(code=‘600519’,start=‘2010-01-01’)
#将互联网上获取的数据存到本地,to_xxx为写入本地
df.to_csv(’./maotao.csv’)
#将本地的文本数据读到df中
df=pd.read_csv(’./maotao.csv’)
#drop中,o表示行,1表示列
df=pd.read_csv(’./maotao.csv’).drop(labels=‘Unnamed: 0’,axis=1)
#将time列转为时间序列类型
df[‘date’]=pd.to_datetime(df[‘date’])
#将date列作为源数据的行索引
df.set_index(‘date’,inplace=True)
ma5=df[‘close’].rolling(5).mean()
ma30=df[‘close’].rolling(30).mean()
#用matplotlib画线
%matplotlib inline
plt.plot(ma5[30:])
plt.plot(ma30[30:])
s1=ma5<ma30#s1死叉
s2=ma5>ma30#s2金叉
df=df[30:]
death_ex=s1&s2.shift(1)#判定死叉的条件
df.loc[death_ex]#死叉对应行数据
death_date=df.loc[death_ex].index#死叉数据对应的日期
golden_ex=-(s1|s2.shift(1))#判定金叉条件
golden_date=df.loc[golden_ex].index#金叉的时间
#1作为金叉的标识
s1=Series(data=1,index=golden_date)
#0作为死叉的标识
s2=Series(data=0,index=death_date)
#s2加到s1中
s=s1.append(s2)
#按日期交叉排好,s存储金叉/死叉对应的事件
s=s.sort_index()
s=s[‘2010’:‘2020’]
#本金
first_money=100000
money=first_money#买卖股票的钱都从money中操作
#hold为持有股票数(100股=1手)
hold=0
for i in range(0,len(s)):#i表示s的Series中的隐式索引
if s[i]==1:#金叉的时间,基于10W买尽可能多的股票
#获取股票对应的开盘价
time=s.index[i]#time为金叉的时间
p=df.loc[time][‘open’]#p为股票的单价,为当天的开盘价
hand_count=money//(p
100)#//为整除,p
100为一手的股票,hand_count为买了多少手
hold=hand_count
100
money-=(holdp)#把买股票的钱从money中减去
else:
#将买入的股票卖出去
#找出卖出股票的单价
death_time=s.index[i]#死叉时间
p_death=df.loc[death_time][‘open’]#卖股票的单价
money+=(p_death
hold)#卖出的股票收入加入到money
#如何判断最后一天为金叉还是死叉
last_money=hold*df[‘close’][-1]#剩余股票的价值
#总收益
money+last_money-first_money
3,各州人口分布分析
import numpy as np
import pandas as pd
from pandas import DataFrame
#导入文件,查看原始数据
abb=pd.read_csv(’./data/state-abbrevs.csv’)#存储的是洲名字state和缩写abb
area=pd.read_csv(’./data/state-areas.csv’)#洲名state和洲面积area
pop=pd.read_csv(’./data/state-population.csv’)#导入各州人口state/region,ages,year,population
abb_pop=pd.merge(abb,pop,left_on=‘abbreviation’,right_on=‘state/region’,how=‘outer’)
#删除重复的abb和state/region中的一项,drop中1为列,0为行
abb_pop.drop(labels=‘abbreviation’,axis=1,inplace=True)#删除abbreviation
#查看存在缺失数据的列
#方式1:isnull(),notnull(),any,all,0表示列
#abb_pop.isnull().any(axis=0)#state和population存在空值
#方式2:.info()
#abb_pop.info()
#找出state中空值对应的简称找到,且对简称进行去重/将state空值对应的行数据取出,从该行数据中就可以取出简称的值
#1,将state中的空值定位
abb_pop[‘state’].isnull()#np.nan
#2.将上述的布尔值作为源数据的行索引取出state为空的行数据
abb_pop.loc[abb_pop[‘state’].isnull()]
#3,将简称取出
abb_pop.loc[abb_pop[‘state’].isnull()][‘state/region’]
#4,对简称去重
abb_pop.loc[abb_pop[‘state’].isnull()][‘state/region’].unique()
#结论:只有PR/USA对应的全称数据为空值
#为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
#不可以用fillna的空的紧邻值填充,要用元素赋值的方式进行填充
#1.先给USA全称对应的空值进行批量赋值(由于只是名没写,故需要写名字)
#1.1将USA对应的行数据找出(行数据中就存在state的空值)
abb_pop[‘state/region’]
‘USA’
abb_pop.loc[abb_pop[‘state/region’]‘USA’]#将对应的行数据取出
#1.2将USA对应的全称空对应的行索引取出
indexs=abb_pop.loc[abb_pop[‘state/region’]
‘USA’].index
abb_pop.iloc[indexs]
abb_pop.loc[indexs,‘state’]=‘United States’
#2.将PR的全称进行赋值
abb_pop[‘state/region’]‘PR’
abb_pop.loc[abb_pop[‘state/region’]
‘PR’]#PR对应的行数据
indexs=abb_pop.loc[abb_pop[‘state/region’]‘PR’].index
abb_pop.loc[indexs,‘state’]=‘PPPRRR’
#-合并各州面积
abb_pop_area=pd.merge(abb_pop,area,how=‘outer’)
#找出area这一列缺失数据,找出哪些是缺失行
abb_pop_area[‘area (sq. mi)’].isnull()
abb_pop_area.loc[abb_pop_area[‘area (sq. mi)’].isnull()]#空值对应的行数据
indexs=abb_pop_area.loc[abb_pop_area[‘area (sq. mi)’].isnull()].index
#取出含有缺失数据的行
abb_pop_area.drop(labels=indexs,axis=0,inplace=True)
#找出2010年的全民人口数据(基于df做条件查询)
abb_pop_area.query('ages
"total"&year==2010’)
#计算各州的人口密度(人口/面积),并加入表中
abb_pop_area[‘tensity’]=abb_pop_area[‘population’]/abb_pop_area[‘area (sq. mi)’]
#排序,并找出人口密度最高的洲,ascending为False表示降序,iloc[0]表示取第一行
abb_pop_area.sort_values(by=‘tensity’,axis=0,ascending=False).iloc[0][‘state’]
#项目三 美国大选献金项目数据分析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值