在数据框上迭代以进行皮尔森测试

时间:2018-12-13 13:22:49

标签: python pandas pearson-correlation

尝试遍历从第二列开始的数据帧,以对收益进行皮埃尔森检验。数据集只是来自Yahoo Finance的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)

1 个答案:

答案 0 :(得分:1)

考虑这个迷你示例:

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'])