0

I can't seem to find how to, in Python 2.7, fill in a variable name instead of an index. I only find answers for other programming languages.

The file that is imported contains multiple columns, but I only want to add columns defined and asked for in the Query function to Data_BM. So how can IlocX be seen as an index.

Even if there is an easier way, I would just like to know how to put a variable on an index position. Thank you in advance.

# function for the datafile name
def datafilename():
    print "Type the data filename, for example: myfile.csv : \n"
    dataname = raw_input()
    print dataname, 'Correct? [Y/N]'
    yn = raw_input()
    if yn == 'Y' or yn == 'y':
        return dataname
    else:
        datafilename()

 # function for the column numbers
def query(var):
    print 'Type column number for',var,':'
    loc = raw_input()
    try:
        loc = int(loc)
    except:
        loc = loc
    print loc, 'Correct? [Y/N]'
    yn = raw_input()
    if yn == 'Y' or yn == 'y':
        if type(loc) == int:
            return loc
        elif loc == 'NA' or loc == 'na':
            return loc
        else:
            print 'Please type column as integer value'
            query(var)
    else:
        query(var)

#Ask for column locations
loc1 = query("X")
loc2 = query("Y")
loc3 = query("Z")
loc4 = query("A")
loc5 = query("B")
Iloc1 = int(loc1-1)
Iloc2 = int(loc2-1)
Iloc3 = int(loc3-1)
Iloc4 = int(loc4-1)
Iloc5 = int(loc5-1)

# Asking for data file name
dataname = datafilename()

#Get data
Data_BM = pd.read_csv(dataname)
Data_BM = Data_BM[Iloc1,Iloc2,Iloc3,Iloc4,Iloc5]

I updated my file, I forgot to put in the query function. The user is asked to fill in the column number for for example X. The user types 1, which is loc1. Iloc 1 = 0. So 0 is the indexnumber. How can I have Iloc1 be the indexnumber in Data_BM = Data_BM[Iloc1,Iloc2,Iloc3,Iloc4,Iloc5] and the same accounts for the others. The reason I want this is because the columns might change but I want to make it easy for the user.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AlmostGr
  • 89
  • 8
  • 1
    What do you mean by "fill in a variable name instead of an index"? Could you provide a more generic example? – Moon Cheesez Jun 07 '16 at 09:02
  • Right now I wrote: Data_BM = Data_BM[Iloc1,Iloc2,Iloc3,Iloc4,Iloc5] Because Iloc1, Iloc2....etc are in my case 1,2,3,8,9. This will be user input. but I want this to be put as index numbers as I did above in Data_BM, but obviously I'm doing it wrong, since it does not work like this:Data_BM = Data_BM[Iloc1,Iloc2,Iloc3,Iloc4,Iloc5]. But how can I make it accept Iloc1, Iloc2...etc as index – AlmostGr Jun 07 '16 at 09:03
  • I think he is thinking of somathing like `$$i` in PHP. I don't think it is possible in python, and should not be. – Paul Jun 07 '16 at 09:04
  • @AlmostGr I deleted my post as I could not edit it to help you. I'm transferring what you said into this comment: I updated my file, I forgot to put in the query function. The user is asked to fill in the column number for example, X. The user types 1, which is `loc1`. `Iloc1 = 0`. So `0` is the index number. How can I have `Iloc1` be the index number in `Data_BM = Data_BM[Iloc1,Iloc2,Iloc3,Iloc4,Iloc5]` and the same accounts for the others. The reason I want this is because the columns might change but I want to make it easy for the user. – Moon Cheesez Jun 07 '16 at 09:30
  • @Moon Cheesez : ' It is possible. – Moon Cheesez' Please let me know how? – AlmostGr Jun 07 '16 at 09:33
  • @AlmostGr I was referring to @PaulHOCHON's PHP version of `$$i`. That would be do use the `exec` command to execute python code with strings. – Moon Cheesez Jun 07 '16 at 09:36
  • I will just use the headers as input, not the column numbers. Thank you all. – AlmostGr Jun 07 '16 at 09:51

1 Answers1

0

Short answer is no,

Long answer is why? If you have a list of variables like a1, a2, a3, a4, ..., an, make a list of them, or a dictionary if you want to access it by strings.

The closest thing you can do is:

vars = {}
for i in range(10):
    vars["Ilock"+str(i)] = something()

vars["Ilock4"] = 42

But it's ugly and not convenient.

Paul
  • 315
  • 1
  • 9