答案 0 :(得分:0)
import pandas as pd
d = ({
'A' : [1,2,3,4,5,6,7,8,9],
'B' : [2,4,6,8,6,4,2,1,4],
})
df = pd.DataFrame(data=d)
df['corr'] = df.index.map(lambda x: df['A'].corr(df.loc[:x, 'B']))
print(df)
A B corr
0 1 2 NaN
1 2 4 1.000000
2 3 6 1.000000
3 4 8 1.000000
4 5 6 0.832050
5 6 4 0.458682
6 7 2 0.000000
7 8 1 -0.301687
8 9 4 -0.262461