All the research I do point to using loc
as the way to filter a dataframe by a col(s) value(s), today I was reading this and I discovered by the examples I tested, that loc
isn't really needed when filtering cols by it's values:
EX:
df = pd.DataFrame(np.arange(0, 20, 0.5).reshape(8, 5), columns=['a', 'b', 'c', 'd', 'e'])
df.loc[df['a'] >= 15]
a b c d e
6 15.0 15.5 16.0 16.5 17.0
7 17.5 18.0 18.5 19.0 19.5
df[df['a'] >= 15]
a b c d e
6 15.0 15.5 16.0 16.5 17.0
7 17.5 18.0 18.5 19.0 19.5
Note: I do know that doing loc
or iloc
return the rows by it's index and the position. I'm not comparing based on this functionality.
But when filtering, doing "where
" clauses what's the difference between using or not using loc
? If any. And why do all the examples I come across regarding this subject use loc
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…