-1

Are the two lines of code equivalent to each other? To me the results seem the same, but I seen libraries use the second method, perhaps there is a reason why?

df['some_column']

df.loc[:, 'some_column']
katiex7
  • 843
  • 10
  • 18
  • 1
    Both are same @katiex7. Where have you seen the use? It might be context specific. – Vishnudev Nov 02 '19 at 19:29
  • I seen it used like so df.loc[:,n] = df.loc[:,n].astype('category').cat.as_ordered() categories = df[n].cat.categories – katiex7 Nov 02 '19 at 19:41
  • He assigns categories as df[n].cat.categories instead of df.loc[:,n].cat.categories so I think they are indeed the same – katiex7 Nov 02 '19 at 19:42

1 Answers1

1

they're equivalent here, but the .loc and .iloc accessors lets you express indexing operations that would be otherwise ambiguous and could lead to it doing the "wrong" thing or would otherwise be confusing readers of your code

Sam Mason
  • 12,674
  • 1
  • 33
  • 48