简体   繁体   中英

Convert dataframe column into binary

My crm data frame contains a column "Reconciled" with numbers from 0 to 130.

I want to convert this column into 0 or 1. If the value is 0, keep 0, otherwise change to 1.

crm['Reconciled'] = crm['Reconciled'].where(crm['Reconciled'] > 0, 1)

Now:

crm['Reconciled'].describe()

Returns:

count     138234
unique         1
top            1
freq      138234
Name: Reconciled, dtype: int64

Hera are alternatives for binary:

crm['Reconciled'] = (crm['Reconciled'] > 0).astype(int)
crm['Reconciled'] = (crm['Reconciled'] > 0).view('i1')
crm['Reconciled'] = np.where(crm['Reconciled'] > 0, 1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM