python - Pandas: Get an if statement/.loc to return the index for that row -


i've got dataframe 2 columns , i'm adding 3rd.

i want 3rd column dependant on value of 2nd either returning set answer or corresponding index row.

an example database below:

print (df)             amount      percentage country       belgium      20           .0952 france       50           .2380 germany      60           .2857 uk           80           .3809 

now want new third column 'other' if percentage below 25% , name of country if percentage above 25%. i've written:

df.['country']='other') df.loc[df['percentage']>0.25, 'country']=df.index 

unfortunately output doesn't give equivalent index; gives index in order:

 print (df)             amount      percentage      country country       belgium      20           .0952         other france       50           .2380         other germany      60           .2857         belgium uk           80           .3809         france 

obviously want see germany across germany , uk across uk. how can give me index in same row number trips threshold in code?

you can try numpy.where:

df['country'] = np.where(df['percentage']>0.25, df.index, 'other') print df          amount  percentage  country country                              belgium      20      0.0952    other france       50      0.2380    other germany      60      0.2857  germany uk           80      0.3809       uk 

or create series index to_series:

df['country']='other' df.loc[df['percentage']>0.25, 'country']=df.index.to_series() print df          amount  percentage  country country                              belgium      20      0.0952    other france       50      0.2380    other germany      60      0.2857  germany uk           80      0.3809       uk 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -