![](/img/trans.png)
[英]How do I modify the value of a pandas dataframe cell that contains a list in it?
[英]How do I properly modify a cell in a dataframe?
我有一個代碼,我正在修改這樣的單元格: IBM["PNL"][2]=3
。 它有效,但它顯示一個警告:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
從我在本文中可以閱讀的內容來看,修改值的正確方法是IBM.loc[2,"PNL"]=3
。 但是,這對我不起作用,並且失敗並出現以下錯誤:
Traceback (most recent call last):
File "<ipython-input-25-10debbad977d>", line 1, in <module>
IBM_dataframe.loc[0,"PNL"]
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1310, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 796, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 922, in _getitem_lowerdim
section = self._getitem_axis(key, axis=i)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1482, in _getitem_axis
self._has_valid_type(key, axis)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1409, in _has_valid_type
key = self._convert_scalar_indexer(key, axis)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 196, in _convert_scalar_indexer
return ax._convert_scalar_indexer(key, kind=self.name)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\tseries\base.py", line 591, in _convert_scalar_indexer
self._invalid_indexer('index', key)
File "C:\Users\menkaur\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 1284, in _invalid_indexer
kind=type(key)))
TypeError: cannot do index indexing on <class 'pandas.tseries.index.DatetimeIndex'> with these indexers [2] of <type 'int'>
現在,我很困惑
我究竟做錯了什么?
既然.ix
已棄用,您有兩種選擇:
IBM.loc[IBM.index[2], "PNL"] = 3
這是基於標簽的索引。 由於您需要標簽但您有位置,因此使用IBM.index[2]
返回標簽。 要么,
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
這是基於位置的索引。 要獲取列PNL
的位置,請使用get_loc
。
該錯誤是因為您嘗試對行使用基於位置的索引,但對列使用基於標簽的索引。 .ix
旨在處理此類情況但不推薦使用。 以下是@jezrael所述的詳細信息。
假設IBM
是pd.DataFrame
, IBM["PNL"]
是pd.Series
。 []
(方括號)調用__getitem__
方法並返回一個系列對象。 然后在IBM["PNL"][2]
返回的系列上調用__getitem__
方法,即[2]
部分。 現在這很好,即使有點令人困惑。 您嘗試分配時出現問題。 IBM["PNL"][2] = 3
告訴pandas
分配給pd.Series
IBM["PNL"]
的第二個元素,它是IBM
數據pd.Series
"PNL"
列的視圖......頭暈目眩?
所以答案是使用適當的索引器直接使用loc
, iloc
, at
, iat
或set_value
分配給IBM
數據幀。
loc
允許您將1-D數組作為索引器傳遞。 數組可以是索引或列的切片(子集),也可以是布爾數組,其長度與索引或列相等。
特別注意:當傳遞標量索引器時, loc
可以分配之前不存在的新索引或列值。
# described by @ayhan
IBM.loc[IBM.index[2], 'PNL'] = 3
iloc
與loc
類似,除了位置而不是索引值。 但是,您無法分配新列或索引。
# described by @ayhan
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
at
與標量索引器的loc
非常相似。 無法對陣列索引器進行操作。 能夠! 分配新的索引和列
IBM.at[IBM.index[2], 'PNL'] = 3
iat
與iloc
類似。 無法在數組索引器中工作。 不能! 分配新的索引和列。
IBM.iat[2, IBM.columns.get_loc('PNL')] = 3
set_value
與標量索引器的loc
非常相似。 無法對陣列索引器進行操作。 能夠! 分配新的索引和列
IBM.set_value(IBM.index[2], 'PNL', 3)
set_value
with takable=True
與iloc
類似。 無法在數組索引器中工作。 不能! 分配新的索引和列。
IBM.set_value(2, IBM.columns.get_loc('PNL'), 3, takable=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.