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.