日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据清洗,筛选

發(fā)布時間:2023/12/19 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据清洗,筛选 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本人在私募,負責數(shù)據(jù)收集以及清洗,就是包括收集數(shù)據(jù),按照領(lǐng)導要求,選出滿足條件的數(shù)據(jù),用于校驗策略是否正確。 現(xiàn)在就在這進行代碼上傳,即用于自己總結(jié)整理,也用于供大家學習了解,實習生到底干什么事。 數(shù)據(jù)收集: #期權(quán)數(shù)據(jù)收集 import akshare as ak import pandas as pd import numpy as np import datetime import time #用于將千分位數(shù)字改為常規(guī)數(shù)字 from locale import atof # 顯示所有列 pd.set_option('display.max_columns', None) # 顯示所有行 pd.set_option('display.max_rows', None) #收集數(shù)據(jù),鐵礦石期權(quán)的全部數(shù)據(jù) start = '2019-02-09' #獲得全部的交易日數(shù)據(jù) dates = ak.tool_trade_date_hist_sina() today = time.strftime("%Y-%m-%d") #篩選符合條件的日期 dates = dates.loc[(dates['trade_date']>start)&(dates['trade_date']<today)] #創(chuàng)建儲存數(shù)據(jù)的DF last = pd.DataFrame() last_2 = pd.DataFrame() #循環(huán)獲取數(shù)據(jù) for i in range(len(dates)):date = dates.iloc[i, 0]#print(date)# 獲取當天所有合約part_1, part_2 = ak.get_dce_option_daily(trade_date= i, symbol="鐵礦石期權(quán)")#print(part_1.head())#print(part_2.head())#增加日期,cp,年份,月份,行權(quán)價,part_1['date'] = datepart_1['cp'] = part_1['合約名稱'].apply(lambda x: x[6:7])part_1['year'] = part_1['合約名稱'].apply(lambda x: x[1:3])part_1['o_month'] = part_1['合約名稱'].apply(lambda x: x[3:5])part_1['xqj'] = part_1['合約名稱'].apply(lambda x: x[8:]) part_1['合約系列'] = part_1['合約名稱'].apply(lambda x: x[0:5])#以日期作為indexpart_1.set_index('date',inplace=True)last = last.append(part_1)part_2.columns = [['合約系列','IV']]#增加日期,年份,月份part_2['date'] = datepart_2.set_index('date')part_2['year'] = part_2['合約系列'].apply(lambda x: x[1:3])part_2['month'] = part_2['合約系列'].apply(lambda x: x[3:])last_2 = last_2.append(part_2) #保存數(shù)據(jù),為方便讀者復現(xiàn),使用csv。(真實情況,直接存入sql數(shù)據(jù)庫,各種報錯,各種坑,數(shù)據(jù)缺失,格式不正確............) last.to_csv('i.csv') last_2.to_csv('i2.csv')

數(shù)據(jù)處理:

df = pd.read_csv('i.csv') #默認期權(quán)持倉量為1,999這種千分位數(shù)字格式。 setlocale(LC_NUMERIC, 'English_US') df['持倉量'] = df['持倉量'].apply(lambda x:atof(x))#根據(jù)每天的主力合約數(shù)據(jù)信息,篩選最虛值合約,分為C,P兩塊,且持倉量最大。 #舉例:今天c端最虛c5800,持倉量1w,為c端最大持倉量,則符合條件保留。 def select_xu(df):df_c = df.loc[df['cp'] == 'C']#根據(jù)行權(quán)價排序,獲得最虛值,C端取最大值。dfc = df_c.sort_values(by='xqj',ascending=True).tail(1) # print(df_c) # print(dfc) # print('-'*20)df_p = df.loc[df['cp']=='P']#根據(jù)行權(quán)價排序,獲得最虛值,p端取最小值。dfp = df_p.sort_values(by='xqj',ascending=True).head(1) # print(df_p) # print(dfp)if max(dfc['持倉量']) == max(df_c['持倉量']) :dfc = dfcelse:dfc = Noneif max(dfp['持倉量']) == max(df_p['持倉量']) :dfp = dfpelse:dfp = Nonereturn dfc ,dfp #目標,里面為日期和年份,月份 dfd = pd.read_excel('I.xlsx') last = pd.DataFrame() last1 = pd.DataFrame() last2 = pd.DataFrame() if __name__ == '__main__':for i in range(len(dfd)):year = dfd.iloc[i,-2]month = dfd.iloc[i,-1]date = dfd.iloc[i,0]#篩選數(shù)據(jù)d = df.loc[(df['year']==int(year))&(df['o_month']==int(month))&(df['date']==datetime.datetime.strftime(date,'%Y-%m-%d'))]#如果數(shù)據(jù)不為空if len(d)>0:dfc,dfp =select_xu(d)if dfc is not None and dfp is not None:s = pd.merge(dfc,dfp)last = last.append(s)elif dfc is None and dfp is not None :s = dfplast1 = last1.append(s)elif dfc is not None and dfp is None :s = dfclast2 = last2.append(s)else:s = Noneelse:passlast.to_csv('i0.csv')last1.to_csv('i1.csv')last2.to_csv('i2.csv')

I.xlsx數(shù)據(jù)內(nèi)容如下:

此時便可獲得滿足條件的期權(quán)數(shù)據(jù)。用于后續(xù)的校驗。

總結(jié)

以上是生活随笔為你收集整理的数据清洗,筛选的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。