Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filter data and replace values in pandas

I am new to pandas and I am trying to replace some values on specific columns of a dataframe. My dataframe looks like this:

    c1  c2  st  mt  ast sr  c7  c8
0   a   a   2   1   4   2   a   a
1   b   b   3   3   3   3   b   b
2   c   c   1   1   2   4   c   c
3   d   d   3   3   1   2   d   d
4   e   e   2   3   2   1   e   e
5   f   f   5   5   5   5   f   f

in row 1, from columns 2 to 5 (st, mt, ast, sr) I have the number 3 in all columns and row 5 I have the number 5 in all columns. When I subset using the code below and try to replace 3 by 0 and 5 by 0 in these columns, the code return all the values in the row replaced by 0, not only the values in those specific columns. I tried:

 df[(df.st == 5) & (df.mt == 5) & (df.ast == 5) & (df.sr == 5)]=0

but I got:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

c1  c2  st  mt  ast sr  c7  c8
0   a   a   2   1   4   2   a   a
1   0   0   0   0   0   0   0   0
2   c   c   1   1   2   4   c   c
3   d   d   3   3   1   2   d   d
4   e   e   2   3   2   1   e   e
5   0   0   0   0   0   0   0   0

I also tried:

df[(df.st == 5) & (df.mt == 5) & (df.ast == 5) & (df.sr == 5)].replace(5,0)

My desired output is:

 c1 c2  st  mt  ast sr  c7  c8
0   a   a   2   1   4   2   a   a
1   b   b   0   0   0   0   b   b
2   c   c   1   1   2   4   c   c
3   d   d   3   3   1   2   d   d
4   e   e   2   3   2   1   e   e
5   f   f   0   0   0   0   f   f

How can I achieve this goal?

>Solution :

Use DataFrame.loc with columnsnames in list:

df.loc[(df.st == 5) & (df.mt == 5) & (df.ast == 5) & (df.sr == 5), ['st','mt','ast','sr']]=0

Another idea is working with only selected lists with comapare by 5 by DataFrame.eq and test if all values Trues by DataFrame.all:

cols = ['st','mt','ast','sr']
df.loc[df[cols].eq(5).all(axis=1) | df[cols].eq(3).all(axis=1), cols]=0

print (df)
  c1 c2  st  mt  ast  sr c7 c8
0  a  a   2   1    4   2  a  a
1  b  b   0   0    0   0  b  b
2  c  c   1   1    2   4  c  c
3  d  d   3   3    1   2  d  d
4  e  e   2   3    2   1  e  e
5  f  f   0   0    0   0  f  f
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading