0

I am learning pandas and want to know the best practice for filtering rows of a DataFrame by column values.

According to https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html, the recommendation is to use optimized pandas data access methods such as .loc

An example from https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html -

df.loc[df['shield'] > 6]

However, according to https://pandas.pydata.org/docs/getting_started/comparison/comparison_with_sql.html#where, a construction like tips[tips['time'] == 'Dinner'] could be used.

Why is the recommended .loc omitted? Is there any difference?

user2309803
  • 541
  • 5
  • 15

1 Answers1

0

With .loc you can also correctly set a value, as not using it raises an you are trying to set a value on a copy of a DataFrame error. For getting something out of your DataFrame, there might be performance differences, but I don't know that.

McToel
  • 131
  • 1
  • 6