Home > Mobile >  Perform operation on specific rows of the dataframe in pandas
Perform operation on specific rows of the dataframe in pandas

Time:02-17

I have a dataframe

df = pd.DataFrame([["A","X",5,4,1],["A","Y",3,1,3],["B","Y",4,7,4],["B","W",3,9,3],["C","Z",7,4,5],["C","Y",1,0,6]], columns=['col1', 'col2', 'col3', 'col4','col5'])

I want to perform the operation only on the rows where "Y" is present in "col 2".

I have a value 9, since y is repeating 3 times, divide 9 by 3 the quotient which we get which is 3 add that value to values of col3 of y rows only.

Similarly, I have value 12, when we divide it by 3 the quotient value 4 needs to be added to the value present in col4 of y rows.

Expected Output:

Out = pd.DataFrame([["A","X",5,4,1],["A","Y",6,5,3],["B","Y",7,11,4],["B","W",3,9,3],["C","Z",7,4,5],["C","Y",4,4,6]], columns=['col1', 'col2', 'col3', 'col4','col5'])

How to do it?

CodePudding user response:

You could use whether "col2" is "Y" or not as a condition in np.where and add values or not depending on it:

cond = df['col2']=='Y'
df['col3'] = np.where(cond, df['col3'] 3, df['col3'])
df['col4'] = np.where(cond, df['col4'] 4, df['col4'])

You could also modify using loc:

df.loc[cond, 'col3']  = 3
df.loc[cond, 'col4']  = 4

Output:

  col1 col2  col3  col4  col5
0    A    X     5     4     1
1    A    Y     6     5     3
2    B    Y     7    11     4
3    B    W     3     9     3
4    C    Z     7     4     5
5    C    Y     4     4     6

CodePudding user response:

You can just check

df.loc[df.col2.eq('Y'),['col3','col4']]  = [3,4]
df
Out[66]: 
  col1 col2  col3  col4  col5
0    A    X     5     4     1
1    A    Y     6     5     3
2    B    Y     7    11     4
3    B    W     3     9     3
4    C    Z     7     4     5
5    C    Y     4     4     6
  • Related