2

When I was originally learning pandas, if I was filtering I'd use the .loc method. I've been told this is incorrect. Given the following dataframe:

data = {'c1':[1,2,3], 'c2':[2,3,4]}
df = pd.DataFrame(data=data, columns=list(data.keys()))

you can get all values where c1 is >= 2 via at least these two methods:

df = df[df['c1']>=2]

or

df = df.loc[df['c1']>=2]

Which is better in what circumstances? When would you use .loc versus not?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Adam Schulz
  • 137
  • 1
  • 1
  • 7

0 Answers0