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?

bash - multiple if clauses. What's wrong with this code?

I'm practicing writing small bash scripts. I have a habit of accidentally typing teh instead of the. So now I want to start at a directory and go through all the files there to find how many occurrences of teh there are. Here's what I have

    #!/bin/bash

    for file in `find` ; do
     if [ -f "${file}" ] ; then
      for word in `cat "${file}"` ; do
       if [ "${word}" = "teh" -o "${word}" = "Teh" ] ; then
        echo "found one"
       fi    
      done
     fi
    done

When I run it, I get

    found one
    found one
    found one
    found one
    found one
    found one
    ./findTeh: line 6: [: too many arguments

Why am I getting too many arguments. Am I not doing the if statement properly?

Thanks

Using XML Mapping Pane with IF Field Code Word Document

I have been trying to use the content control from XML Mapping Pane with the IF Field Code. But I am having some issues. It seems that i am not being able to compare the value of the content control with what i want

For example:

{ IF Language_CodeValue <> "DAN" "ENGLISH" "DANISH" } 

Language_CodeValue being a content control from the XML Mapping Pane. Any help would be appreciated. The Language_CodeValue do contain DAN in it when i display it on a word document. Seems like the comparison with string which is not working

= and == difference in If statement Java

I am facing something strange here. Please help me understand if I am missing something. My if condition was supposed to be:

if(configuredPdf == true)

But by mistake I had written:

if(configuredPdf = true)

And my Eclipse compiler does not ask me to correct it. Then I assume there is no compile time or checked exception. So:

(configuredPdf = true)

Returns a boolean?

Is there a way to generate a conditional expression which can be directly executed by if?

For example, can I create a method which can return me an expression that can be evaluated by if?

function getCondition(variable, value, operator)//not sure what params to pass
{
   var condition = false; //initialized to false
   //generate condition based on parameter passed
   return condition;
}

and then use it directly

if ( getCondition( a, 5, "<" ) ){ console.log("correct") }

VB.NET - 'If Value <>' vs 'If Not Value ='

I understand that both of the following do the same thing

If Value <> and

If Not Value =

But I noticed the other developer at my job seems to use

If Not Value =

I was wondering if there is any difference in the logic or efficiency between them and if so which one would be the better one to use.

Python: why doesn't if(a == 0): a = 1 if(a == 1): a = 0 work for making a toggle

I'm working on a project using python to read the digital inputs on the raspberry pi. I wanted to turn one of the buttons into a toggle, as in it switches a value between 1 and 0 whenever I press it. Everything is working fine except the section:

if(a == 0.0):
    a = 1.0
if(a == 1.0):
    a = 0.0

It seems like this should work with the rest of the code to make the value toggle between 1 and 0 whenever the button is pressed, but a prints as 0.0 every time, does anyone know why this is?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
a = 0.0
b = 0.0
c = 0

while True:
    if(GPIO.input(4) ==1 and c ==0):
        print 'Button 1 Pressed'
        if(a == 0.0):
            a = 1.0
        if(a == 1.0):
            a = 0.0
        c = 1
        print a
    if(GPIO.input(4) !=1):
        c = 0
    if(GPIO.input(24) ==0):
        print 'Button 2 Pressed'