python - Pandas map by index value without dummy column -


i have dataframe df 100 rows , 2 columns, rowid , probability. df sorted in descending order according probability (e.g. 0.997, 0.973, 0.960, etc), , index of df in ascending order (0, 1, 2, etc).

i map values in probability column 's' 'success' first 10 values, , 'f' 'fail' rest. this, create dummy column called index, apply transformation, , drop dummy column.

df['index'] = range(0, 100) df['probability'] = df[['probability', 'index']].apply(lambda x:                                                        's' if x['index'] < 10                                                        else 'f', axis=1) df_result.drop(['index'], axis=1) 

is there way can without creating dummy column?

if index 0...n work:

df['probability'] = np.where(df.index < 10, 's', 'f') 

if you're not sure index in order, this?

 df.loc[df.index[:10], 'probability'] = 's'  df.loc[df.index[10:], 'probability'] = 'f' 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -