这是我第一次尝试发布问题。如果没有很好地描述,很抱歉。
我使用panda对日期时间进行了延迟/偏移,并创建了结果的相关矩阵。然后,我输出顶部(90(%相关性的数据帧,并显示它们,如下所示。
数据显示为这样,其中c1+c2中的_number是滞后/移位周期。(_number=_lag/班次(
c1 c2 %corr
0 Atype Atype 1.000000
1 Ctype Dtype 0.96
2 Btype_3 Etype_3 0.96
3 Btype Etype 0.95
4 Ctype_1 Atype_2 0.93
5 Atype_1 Atype_1 0.93
6 Dtype_2 Etype_4 0.92
7 type_1 Atype_5 0.91
我只对滞后/偏移的价值感兴趣。我希望消除所有具有相同滞后/偏移的东西(包括0值-没有任何滞后(。像这样出现
c1 c2 %corr
0 Ctype_1 Atype_2 0.93
1 Dtype_2 Etype_4 0.92
2 Etype_1 Atype_5 0.91
如何做到这一点?
您可以用str.extract
提取每列的滞后,并用不同的值对行进行切片:
lag1 = df['c1'].str.extract('_(.*)', expand=False).fillna('')
lag2 = df['c2'].str.extract('_(.*)', expand=False).fillna('')
df2 = df[lag1 != lag2]
输出:
c1 c2 %corr
4 Ctype_1 Atype_2 0.93
6 Dtype_2 Etype_4 0.92
7 type_1 Atype_5 0.91
使用str.replace
:的替代方案
lag1 = df['c1'].str.replace('^[^_]+', '', regex=True)
lag2 = df['c2'].str.replace('^[^_]+', '', regex=True)
df[lag1 != lag2]
注意。注意,根据您的数据,采取上游行动并只计算不同滞后数据的相关性可能更有效