简体   繁体   中英

Compare each row to the other rows in a Dataframe

I have a dataframe containing a column of values (X).

df = pd.DataFrame({'X' : [2,3,5,2]})

For each row, I would like to find the average of the X values from the other rows (A).

在此处输入图片说明

其他行的平均值是列的总和减去行值除以列的大小减去1。在Pandas中,其写为​​:

df['A'] = (df['X'].sum() - df['X'])/(len(df) - 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM