0

A bit of pickle. I am trying to find a single row in a dataframe by searching for a specific string value, then replace the string value with zero. I am using the following code:

selectRow = df[df.iloc[:,0].str.match(ID)]

selectRow.replace(ID, 0)

where ID is some string. This returns one row within the dataframe as expected. However, I can't seem to modify the values of the selectRow in dataframe, as the selectRow is a separate dataframe at this point.

I know I am missing something incredibly basic. Any help would be much appreciated!

CN

4
  • P.S. dataframe I am working on is without header, and I would prefer to keep it that way - ideally. Commented Oct 14, 2019 at 18:08
  • you can just assign it in your first call provided you know which column it is you want to change based on index Commented Oct 14, 2019 at 18:10
  • after replace, try update selectRow back to df as: df.update(selectRow) Commented Oct 14, 2019 at 18:14
  • Sorry, could you elaborate on "assigning it to the first call"? I've tried df.update but it didn't make any changes. :( Commented Oct 14, 2019 at 18:16

2 Answers 2

1

You could do it in a single line as follows:

Method-1:

ID = `eee`
df.iloc[:,0].loc[df.iloc[:,0]==ID] = 0

Output:

        C1   C2
0      aaa  bbb
1      ccc  ddd
2        0  fff
3  ggg eee  hhh

Method-2:

Use pandas.Dataframe.replace method.

df.iloc[:,0].replace(to_replace=ID, value=0, regex=False)

Output:

        C1   C2
0      aaa  bbb
1      ccc  ddd
2        0  fff
3  ggg eee  hhh

If you set regex=True, that could adjust the rows based on other occurrences of the ID as well. Output:

        C1   C2
0      aaa  bbb
1      ccc  ddd
2        0  fff
3        0  hhh

Dummy Data

df = pd.DataFrame([['aaa','bbb'],['ccc','ddd'],['eee','fff'],['ggg eee','hhh']], columns=['C1','C2'])
print(df)

Output:

        C1   C2
0      aaa  bbb
1      ccc  ddd
2      eee  fff
3  ggg eee  hhh
Sign up to request clarification or add additional context in comments.

Comments

0

test data:

import pandas as pd
df = pd.DataFrame([['aaa','bbb'],['ccc','ddd'],['eee','fff'],['ggg','hhh']])

df is:

    Out[42]: 
         0    1
    0  aaa  bbb
    1  ccc  ddd
    2  eee   fff
    3  ggg  hhh

code:

Template = 'eee'
df[0] = [(lambda x: 0 if (x == Template) else x)(x) for x in df[0]]

Result is:

Out[45]: 
         0    1
    0  aaa  bbb
    1  ccc  ddd
    2    0  fff
    3  ggg  hhh

Does it help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.