mardi 1 mars 2016

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

I've got a dataframe with 2 columns and I'm adding a 3rd.

I want the 3rd column to be dependant on the value of the 2nd either returning a set answer or the corresponding index for that row.

An example the database is below:

    print (df)
                Amount      Percentage
    Country      
    Belgium      20           .0952
    France       50           .2380
    Germany      60           .2857
    UK           80           .3809

Now I want my new third column to say 'Other' if the percentage is below 25% and to say the name of the country if the percentage is above 25%. So this is what I've written:

    df.['Country']='Other')
    df.loc[df['percentage']>0.25, 'Country']=df.index

Unfortunately my output doesn't give the equivalent index; it just gives the 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 I want to see Germany across from Germany and UK across from UK. How can I get it to give me the index which is in the same row as the number which trips the threshold in my code?

Aucun commentaire:

Enregistrer un commentaire