2

Are the following two items equivalent?

df['studio'] == df.studio

I thought that it would return a True/False based on a comparison of the entire object, but when doing this, it seems to return another pd.Series with a bunch of True/False values so it's hard (for me at least) to tell if the two items are the same thing or not.

If they are the same, is one notation preferred over another? If they are not, how are the two different?

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 2
    https://stackoverflow.com/questions/41130255/for-pandas-dataframe-whats-the-difference-between-using-squared-brackets-or-do. `.`. a is convenience method and has several pitfalls/limitations, so brackets are generally better. – ALollz Aug 15 '20 at 21:34

1 Answers1

2

df['studio'] and df.studio are the same thing. However, df['studio'] is preferred because:

  • Indexing (such as slicing) uses the 'bracket notation' ( [ ]'s )
  • Using bracket notation is much more common and universal among languages and lets other developers read your code easier

I'm assuming when you ran df['studio'] == df.studio you got a series of True True True etc.

That's because you are comparing the studio column, to the studio column. You could also do the following: df[df['studio'] == df['studio']]

which returns all of the rows (instead of True/False), essentially saying: Return to me all the rows where the studio column is equivalent to the studio column (which, of course, is always.)

Landon
  • 119
  • 11