I have this list with five heights in it and I want to put it in a loop to create five separate dataframes indexed by these numbers. This would include creating a column name based on different height, reading a csv file and assigning the colNames to it, and finally dropping the unused columns. I have multiple blocks of the same code to do this but I want to learn how to do it with a loop so I can clean up my script.
I get a NameError: name 'colNames' is not defined.
i = 0
height = ['0', '5', '15', '25', '50']
while i < len(height):
colNames["height{}".format(i)] = ["A", "B_%s" % height, "C", "D"]
df["height{}".format(i)] = pd.read_csv("test%s.csv" % height, names = colNames["height{}".format(i)])
df["height{}".format(i)].drop(labels = ["A", "C"],axis = 1, inplace = True)
i += 1
Expected results
colNames0 = ["A", "B_0", "C", "D"]
df0 = pd.read_csv("test0.csv", names = colNames0])
df0.drop(labels = ["A", "C"], axis = 1, inplace = True)
...
colNames50 = ["A", "B_0", "C", "D"]
df50 = pd.read_csv("test50.csv", names = colNames50])
df50.drop(labels = ["A", "C"], axis = 1, inplace = True)