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
Post a Comment