Q:

Python df.loc not working for variables

I am fairly new to Python, especially pandas. I have a dataframe called KeyRow which is from a bigger df:
KeyRow=df.loc[df['Order'] == UniqueOrderName[i]]

Then I make a nested loop
for i in range (0,len(PersonNum)):
   print(KeyRow.loc[KeyRow['Aisle'] == '6', 'FixedPill'])
So it appears to only work when a constant is placed, whereas if I use PersonNum[0] instead of '6',even though both values are equivalent, it appears not to work. When I use PersonNum[i] this is the output I am getting:
Series([], Name: FixedPill, dtype: object)

Whereas if I use 'x' I get a desired result:
15    5
Name: FixedPill, dtype: object

Thanks for all the help in advance.

A:

It's a little unclear what your are trying to accomplish with this questions. If you are looking to filter a DataFrame, then I would suggest to never do this in an iterative manner. You should take full advantage of the slicing capabilities of .loc. Consider the example:
df = pd.DataFrame([[1,2,3], [4,5,6],
                   [1,2,3], [2,5,6],
                   [1,2,3], [4,5,6],
                   [1,2,3], [4,5,6]], 
                  columns=["A", "B", "C"])
df.head()

    A   B   C
0   1   2   3
1   4   5   6
2   1   2   3
3   2   5   6
4   1   2   3

Suppose you have a list of PersonNum that you want to use to locate the a particular field where your list is PersonNum = [1, 2]. You can slice the DataFrame in one step by performing:
df.loc[df["A"].isin(PersonNum), "B"]

Which will return a pandas Series and 
df.loc[df["A"].isin(PersonNum), "B"].to_frame()

which returns a new DataFrame. Utilizing the .loc is significantly faster than an iterative approach.

