Given a pandas dataframe, whats the most efficient way to do row wise difference of rolling mean. i.e. if we have a dataframe as:
np.random.seed(43)
df = pd.DataFrame(np.random.randint(0,10,size=(6, 4)), columns=list('ABCD'))
which generated the dataframe as such:
A B C D
0 4 0 1 5
1 0 3 1 2
2 7 0 3 2
3 9 1 2 2
4 3 5 4 4
5 0 5 8 0
then the expected output is:
row_diff[0] = [row[0]['A'] - mean(row[1:]['A']), row[0]['B'] - mean(row[1:]['B']), row[0]['C'] - mean(row[1:]['C']), [row[0]['D'] - mean(row[1:]['D'])]]
row_diff[1] = [row[1]['A'] - mean(row[2:]['A']+row[0]['A']), row[1]['B'] - mean(row[2:]['B']+row[0]['B']), row[1]['C'] - mean(row[2:]['C']+row[0]['C']), [row[0]['D'] - mean(row[2:]['D']+row[0]['D'])]]
and so on.
question from:
https://stackoverflow.com/questions/65944024/row-wise-mean-difference-in-a-pandas-dataframe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…