MRpapas
MRpapas

Reputation: 23

How to set a variable in if-statement?

Ι have a problem with my code. I run it and in command window shows me:

NameError: name 'IA' is not defined (IA is the equation I show you in my code solution above)

My code:

import math
import pandas as pd
import matplotlib as mpl
import numpy as np

dfArxika = pd.read_csv('AIALL.csv', usecols=[0,1,2,3,4,5,6,7,8,9,10], header=None, index_col=False)
print(dfArxika.columns)

A=dfArxika[9]

for i in range(len(A)):
    if (A[i] >= 4.8 and A[i] < 66):
        IA[i]= (2.2*(math.log10(A[i]/66))+5.5)
    elif (A[i] >= 66):
        IA[i]= 3.66*(math.log10(A[i]/66)+5.5)
    else:
        IA[i]=(2.2*(math.log10(A[i]/66))+5.5)

How can I fix it? I mean how can I find values of IA variable for each value of column?

To sum up, I would like to find a new variable (IA), which is based in A variable values

Upvotes: 0

Views: 75

Answers (4)

Shad0w
Shad0w

Reputation: 345

might be a typo on your variable on the 10th A=dfArxika[9] try changing it to IA=dfArxika[9] or change your code to

for i in range(len(A)):
    if (A[i] >= 4.8 and A[i] < 66):
        A[i]= (2.2*(math.log10(A[i]/66))+5.5)
    elif (A[i] >= 66):
        A[i]= 3.66*(math.log10(A[i]/66)+5.5)
    else:
        A[i]=(2.2*(math.log10(A[i]/66))+5.5)
```.

Upvotes: 0

Robin Thibaut
Robin Thibaut

Reputation: 640

You need to define IA before the loop.

Basically, you are trying to put numbers in a list that doesn't exist.

What you could do is make a copy of A and name it IA:

import math
import pandas as pd
import matplotlib as mpl
import numpy as np

dfArxika = pd.read_csv('AIALL.csv', usecols=[0,1,2,3,4,5,6,7,8,9,10], header=None, index_col=False)
print(dfArxika.columns)

A=dfArxika[9]
IA=np.copy(A)

for i in range(len(A)):
    if (A[i] >= 4.8 and A[i] < 66):
        IA[i]= (2.2*(math.log10(A[i]/66))+5.5)
    elif (A[i] >= 66):
        IA[i]= 3.66*(math.log10(A[i]/66)+5.5)
    else:
        IA[i]=(2.2*(math.log10(A[i]/66))+5.5)

Upvotes: 0

wasif
wasif

Reputation: 15480

You have a typo A should be IA, also for this the variable seems not to be declared:

import math
import pandas as pd
import matplotlib as mpl
import numpy as np

dfArxika = pd.read_csv('AIALL.csv', usecols=[0,1,2,3,4,5,6,7,8,9,10], header=None, index_col=False)
print(dfArxika.columns)

IA=dfArxika[9]

for i in range(len(A)):
    if (A[i] >= 4.8 and A[i] < 66):
        IA[i]= (2.2*(math.log10(A[i]/66))+5.5)
    elif (A[i] >= 66):
        IA[i]= 3.66*(math.log10(A[i]/66)+5.5)
    else:
        IA[i]=(2.2*(math.log10(A[i]/66))+5.5)

Upvotes: 0

Marc Sances
Marc Sances

Reputation: 2614

I think you got a typo here:

A=dfArxika[9]

that should be

IA=dfArxika[9]

Upvotes: 1

Related Questions