0

I tried to change the values in those columns which dtypes are numeric values. But my code doesn't work.

dataframe name: mergedDF

for c, dtype in zip(mergedDF.columns,mergedDF.dtypes):
    if dtype == np.int64:
        mergedDF[c].where(mergedDF[c]> 0,1)

It basically changed nothing. When I tried to assign variable names it changed everything to 1

if dtype == np.int64:
    mergedDF[c] = mergedDF[c].where(mergedDF[c]> 0,1)

input:

col1   col2   col3
1       2       0
4       0       1
2       2       0

expected output:

col1   col2   col3
1       1       0
1       0       1
1       1       0

Only 2 dtypes in my original table.

case_history            object     # text column
cleaned_text            object     # text column
401k deduction           int64     # 0-10
assistance manual        int64     # 0-10
assistance quick         int64     # 0-10
3
  • can you print the contents of dtype? Commented Oct 23, 2019 at 15:03
  • @Yuca There are only two dtypes in the original dataset. Commented Oct 23, 2019 at 15:45
  • for a solution along the lines of your initial efforts, look at this: stackoverflow.com/questions/3501382/… Commented Oct 23, 2019 at 17:00

1 Answer 1

1

You can do select_dtypes + clip then update

df.update(df.select_dtypes(include=np.number).clip(upper=1))
Out[118]: 
   col1  col2  col3
0     1     1     0
1     1     0     1
2     1     1     0
Sign up to request clarification or add additional context in comments.

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.