0

I have a data frame (called keys) containing names and other info; I made the names the indices. Now there is an array containing names, and I want to pass this array into a for loop that will loop through the keys dataframe and append the rows with the name from the for loop as the index to an empty dataframe; I then want the program to return this new dataframe that contains only the info from the keys dataframe of the names from the array.

keys = pd.read_excel(FILE, sep = 'delimiter', skiprows = 5, header = None, names = colnames1)
keys.set_index('Name', inplace = True)
array = ['Name1', 'Name2']
result = pd.DataFrame()
for i in range(len(array)):
    result = result.append(keys.set_index(i))

I get the error:

KeyError: 0

I know that the for loop isn't correct; does anyone have a solution?

sentence
  • 8,213
  • 4
  • 31
  • 40
falalalalala
  • 19
  • 1
  • 4
  • 5
    For `pandas` specific questions you will get faster/better answers if you provide an example of your dataset and how your expected output looks like, find more information about a _good pandas questions_ [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Erfan Jun 03 '19 at 17:35
  • Second the comment that a sample of your output would be very helpful. As it stands, I can't picture what you're trying to accomplish with this code. One thing to look at, though, the keyerror is because you're iterating over the length of the index list rather than the items in it – G. Anderson Jun 03 '19 at 17:50

0 Answers0