Python_数据分析_pandasPython

dataframe

2021-12-21  本文已影响0人  Miss_Sissi
a.to_csv("E:/factor/profit/%d.csv"%DFNUM)

各种把循环语句变成矩阵运算的方法

减去之前的一个数

a['a']-a['a'].shift(1)

协方差、相关系数计算

a['F'] = a['A'].rolling(8).cov(a['B'])
a['F'] = a['A'].rolling(8).corr(a['B'])

取两列的最小值

a['F'] = a[['A','B']].min(axis=1)

算回归系数和残差

temp = ((RollingOLS(endog=a['A'],exog=sm.add_constant(a['B']),window=7)).fit()).params
a['R'] = (a['A'] - (temp['B']*a['B']+temp['const'])).rolling(7).sum()

apply函数

LINE['x1'] = LINE.apply(lambda x: x.PreClose if x.TradingTime == 1000 else x.pre, axis=1)

分组计算

a = a.set_index(['A', 'B'])
a['R'] = a['T'].groupby(['A','B']).rank(method='min',ascending=False)

index

去掉index,加上index,交换index

test = test.reset_index(drop=False)
test = test.set_index(['SecuCode','TradingDay'])
test = test.swaplevel()

多重index排序

test = test.sort_index(level=[0,1])

多重index切片

a.loc[(date[T+1:T+8],20140102),['ClosePrice'])
df = df.reset_index(drop=False)
df = pd.merge(df, I, left_on="A", right_on="B", how="right")
test = test.rename(columns={'con_date':'T','stock_code':'S'})

缺失值

.replace([np.inf, -np.inf, np.nan], 0)

补齐缺失值

factor['A'].fillna(method ='ffill', inplace = True)

截取缺失值

a[a.isna().any(axis=1)]
z = np.full((len(c),len(d)), (d)).T.reshape(len(c)*len(d))
z = pd.DataFrame(z, columns=['T'])
上一篇下一篇

猜你喜欢

热点阅读