問題:
Can someone explain how these two methods of slicing are different?有人可以解釋這兩種切片方法有何不同嗎?
I've seen the docs , and I've seen these answers , but I still find myself unable to understand how the three are different.我看過文檔,也看過這些答案,但我仍然發現自己無法理解這三者有何不同。 To me, they seem interchangeable in large part, because they are at the lower levels of slicing.對我來說,它們在很大程度上似乎可以互換,因爲它們處於較低的切片級別。
For example, say we want to get the first five rows of a DataFrame
.例如,假設我們想要獲取DataFrame
的前五行。 How is it that these two work?這兩個是如何工作的?
df.loc[:5]
df.iloc[:5]
Can someone present three cases where the distinction in uses are clearer?有人可以提出三種用法區別更清楚的情況嗎?
Once upon a time, I also wanted to know how these two functions differ from df.ix[:5]
but ix
has been removed from pandas 1.0, so I don't care anymore.曾幾何時,我也想知道這兩個函數與df.ix[:5]
不同,但ix
已從 pandas 1.0 中刪除,所以我不再關心了。
解決方案:
參考一: https://stackoom.com/question/28Ypl參考二: How are iloc and loc different?