Chethan
Chethan

Reputation: 611

Replace the values of column with mean which has particular value in other column in pandas

I have a data frame

df = pd.DataFrame([[10, -1], [20, 1], [30, -1],[40, 1], [50, 1], [60, 1], [70,-1], [80,-1], [90,-1], [100,1]], columns=['A', 'B'])

Calculate the mean of column A whole value is 1 in column B and assign to c, here it is c=54. I want to replace only those values of column A which has -1 value in column B and value lesser than c with c value.

Expected Output:

df = pd.DataFrame([[54, -1], [20, 1], [54, -1],[40, 1], [50, 1], [60, 1], [70,-1], [80,-1], [90,-1], [100,1]], columns=['A', 'B'])

How to do it?

Upvotes: 1

Views: 62

Answers (1)

jezrael
jezrael

Reputation: 862601

First get values of A by B==1 in DataFrame.loc and boolean indexing, get mean and set A by compare less like mean only for B==-1 values:

mean = df.loc[df['B'].eq(1), 'A'].mean()
print (mean)
54.0

mask = df['A'].lt(mean) & df['B'].eq(-1)
df.loc[mask, 'A'] = mean
print (df)
     A  B
0   54 -1
1   20  1
2   54 -1
3   40  1
4   50  1
5   60  1
6   70 -1
7   80 -1
8   90 -1
9  100  1

Upvotes: 2

Related Questions