R. Cox
R. Cox

Reputation: 879

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).

enter image description here

Upvotes: 0

Views: 210

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148910

The mean of the other rows is the sum of the column minus the row value divided by the size of the column minus 1. In Pandas it writes:

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

Upvotes: 4

Related Questions