试图循环通过从第二列开始的数据帧,以对返回进行pearsonr测试。数据集只是来自雅虎金融的nvidia
df=pd.read_csv('NVDA.csv',dtype={'label':str})
for column in df.loc[:,0:3]:
pearson_coefficient,p_value=pearsonr(column,df['Volume'])
print('Pearson Coefficient: ',pearson_coefficient)
考虑这个小例子:
In [10]: df = pd.DataFrame(np.random.randint(10, size=(6,4)))
In [11]: [col for col in df.loc[:, 0:3]]
Out[11]: [0, 1, 2, 3]
请注意,形式为for col in df
的循环迭代列标签,而不是Series中的列值。所以使用
for column in df.columns[0:3]:
pearson_coefficient, p_value = pearsonr(df[column],df['Volume'])