是否可以用Python以列而不是行的方式与一个固定系列进行关联?

问题描述

两个月前(Is it possible to do running correlation with one fixed series in Python?),我在这里问了一个问题,我从同一个用户那里得到了很大的帮助。

我的目标是与Pandas中的一个固定序列进行关联。这可以通过下面的代码逐行实现。但是,我的目标是按列进行此操作。因此,我不想为每一行测试相关性,而是要为每列运行一个相关性测试。任何帮助表示赞赏!

#Method to do running correlation rowwise.
import pandas as pd
from scipy.stats import pearsonr 
import numpy as np 

# running correlation,one fixed series (rows)
df1 = pd.DataFrame([1,3,2,4,5,6,1,8,8])
df2 = pd.DataFrame([1,2])

CORR_VALS = df2[0].values
def get_correlation(vals):
    return pearsonr(vals,CORR_VALS)[0]

df1['correlation'] = df1.rolling(window=len(CORR_VALS)).apply(get_correlation)

但是,我的目标是按列进行操作。

然后我的数据如下所示:

values = {'column1': [7,1],'column2': [1,3],"column3" : [2,5]
        }

df1 = pd.DataFrame(values)

df2 = pd.DataFrame([2,3])

print(df1)
   column1  column2  column3
0        7        1        2
1        2        5        5
2        3        2        2
3        1        3        5

print(df2)
   0
0  2
1  3
2  4
3  3

我想要的输出是这样的:

Out[71]: 
          0
0 -0.620920
1  0.239046
2  0.000000

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)