
[問題] 有關df.loc[]的問題

如果我今天要找出Store 1中的Cost
df.loc["Store 1"]["Cost"]
老師說用這種方法好像會出現問題因此不建議
原文如下:
This looks pretty reasonable and gets us the result we wanted. But chaining
can come with some costs and is best avoided if you can use another approach.
In particular, chaining tends to cause Pandas to return a copy of the
DataFrame instead of a view on the DataFrame. For selecting a data, this is
not a big deal, though it might be slower than necessary. If you are changing
data though, this is an important distinction and can be a source of error.
想請教大家,老師在這邊講的具體上來說是什麼問題?
小的有點看不懂這樣的方法會帶來什麼狀況@@
不知道是否有前輩可以幫忙指點一二?
--
◤ ◤ ◣
● Ο ο ◤ ◣ ◣
◣ ◤ 。 ο ○ 。 ○ °●
◣ ≡ ◤ ° ο Ο ◣ ◤
◤ ◣ ≡ ◤
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.163.71.122
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1543927149.A.227.html
推
12/04 21:25,
6年前
, 1F
12/04 21:25, 1F
→
12/04 21:25,
6年前
, 2F
12/04 21:25, 2F
→
12/04 21:26,
6年前
, 3F
12/04 21:26, 3F
→
12/04 21:28,
6年前
, 4F
12/04 21:28, 4F
→
12/04 21:28,
6年前
, 5F
12/04 21:28, 5F
→
12/04 21:30,
6年前
, 6F
12/04 21:30, 6F
→
12/04 21:31,
6年前
, 7F
12/04 21:31, 7F
感謝gmccntzxl前輩的分享,
我剛剛研究了一下,我的理解大致上是這樣:
chain indexing容易出現問題的狀況是在賦值時,
兩個中括號放在一起時,
第一個中括號的工作(取值)
但是取值後返回的不一定是view或是copy(依照內存狀況不一定)
所以當在處理第二的中括號(賦值)時,
若第一個返回的是copy就有可能會產生SettingWithCopy
這也是為什麼chain indexing這麼不穩定的原因
不知道我這樣的理解是否正確?
※ 編輯: sssh (1.163.71.122), 12/04/2018 23:49:08
推
12/05 00:48,
6年前
, 8F
12/05 00:48, 8F
→
12/05 00:49,
6年前
, 9F
12/05 00:49, 9F
→
12/05 00:51,
6年前
, 10F
12/05 00:51, 10F
→
12/05 00:51,
6年前
, 11F
12/05 00:51, 11F
→
12/05 00:53,
6年前
, 12F
12/05 00:53, 12F
→
12/05 00:54,
6年前
, 13F
12/05 00:54, 13F
→
12/05 00:59,
6年前
, 14F
12/05 00:59, 14F
→
12/05 01:01,
6年前
, 15F
12/05 01:01, 15F
→
12/05 01:03,
6年前
, 16F
12/05 01:03, 16F
→
12/05 01:04,
6年前
, 17F
12/05 01:04, 17F
→
12/05 01:06,
6年前
, 18F
12/05 01:06, 18F
→
12/05 01:07,
6年前
, 19F
12/05 01:07, 19F
→
12/05 01:11,
6年前
, 20F
12/05 01:11, 20F
→
12/05 01:14,
6年前
, 21F
12/05 01:14, 21F
推
12/05 10:12,
6年前
, 22F
12/05 10:12, 22F
→
12/05 10:32,
6年前
, 23F
12/05 10:32, 23F
推
12/06 17:05,
6年前
, 24F
12/06 17:05, 24F
→
12/06 17:05,
6年前
, 25F
12/06 17:05, 25F
→
12/06 17:19,
6年前
, 26F
12/06 17:19, 26F
Python 近期熱門文章
PTT數位生活區 即時熱門文章