0

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
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Questions
  • 117
  • 3
  • 3
  • 9
  • 2
    For starters, `range (0,len(PersonNum))` is returning `int` while `'6'` is a string so you'll probably be seeing `TypeError: invalid type comparison`. – Andrew L Jun 30 '17 at 15:33
  • That fixed things. I made PersonNum into a string. Appreciate it! – Questions Jun 30 '17 at 16:50

1 Answers1

4

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.

RMatt
  • 128
  • 1
  • 10