简体   繁体   English

将 numpy 向量化函数中的数据帧作为参数传递

[英]pass dataframe in numpy vectorization function as an argument

I have a dataframe with 30 columns.我有一个包含 30 列的数据框。 I am passing my dataframe to the function and returning values through numpy vectorization.我将我的数据帧传递给函数并通过 numpy 向量化返回值。 However it`s not working and giving me an error that invalid index to scalar variable.但是它不起作用,并给我一个错误,标量变量的索引无效。 T2 is constant with value 5000 T2 为常数,值为 5000

def get_short_incl_MC_rules(df,T2):      
    return 'True' if(df['yield_rank'] < T2 and df['active_events_and_earnings'] == 1 and df['market_cap'] > 500 and df['net_income'] > 0) else False

vectFunc = np.vectorize(get_short_incl_MC_rules)
list(vectFunc(df,T2))

Chain boolean masks together with & for bitwise AND , it is called boolean indexing and it is vectorized operation.将布尔掩码与&一起用于按位AND ,称为boolean indexing ,它是矢量化运算。 Also for improving performance is added values for comparing by numpy arrays:此外,为了提高性能,还增加了用于通过 numpy 数组进行比较的values

def get_short_incl_MC_rules(df,T2):      
    return (df['yield_rank'].values < T2) & 
           (df['active_events_and_earnings'].values == 1)  & 
           (df['market_cap'].values > 500)  & 
           (df['net_income'].values > 0)

out = get_short_incl_MC_rules(df,T2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM