简体   繁体   中英

Assign values in new column on condition pandas

i have a dataframe:

id    name    value
 1    asd      0.5
 2    fgg      0.8
 3    hfd      1.5
 4    erw      0.5

i have to create a new column accept such that, if the value is greater than 1.0, make outlier as 1, else 0.

id    name    value   accept
 1    asd      0.5      0
 2    fgg      0.8      0
 3    hfd      1.5      1
 4    erw      0.5      0

i can do it using iterrows and use .loc.

for index,row in df.iterrows():
    if row['value']>1:
        df.loc[df.index==row.index,'accept'] = 1
    else:
        df.loc[df.index==row.index,'accept'] = 0

Is there a simpler way of doing this without iterating?

Condition cast to int - True s are converted to 1 and False s to 0 :

df['accept'] = (df['value'] > 1).astype(int)
print (df)
   id name  value  accept
0   1  asd    0.5       0
1   2  fgg    0.8       0
2   3  hfd    1.5       1
3   4  erw    0.5       0

For another values use numpy.where :

df['accept'] = np.where(df['value'] > 1, 'high', 'low')
print (df)
   id name  value accept
0   1  asd    0.5    low
1   2  fgg    0.8    low
2   3  hfd    1.5   high
3   4  erw    0.5    low

Use np.floor + astype(int) as long as your values are between 0 and 2.

df['accept'] = np.floor(df.value).astype(int)
df

   id name  value  accept
0   1  asd    0.5       0
1   2  fgg    0.8       0
2   3  hfd    1.5       1
3   4  erw    0.5       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