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
Jiayu Zhang
  • 719
  • 7
  • 22

1 Answers1

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
BENY
  • 317,841
  • 20
  • 164
  • 234