Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

numpy - Row wise mean difference in a Pandas DataFrame

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm always confused about the best way to iterate in Pandas. Maybe something like matrix subtraction?

result = df - ((df.sum(axis=0) - df)/(len(df) - 1))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...