I've got a code where I'm modifying cells like this: IBM["PNL"][2]=3
. It works, but it shows up a warning:
我有一个代码,我正在修改这样的单元格: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
From what I can read in the article, a proper way of modifying the value would had been IBM.loc[2,"PNL"]=3
. However, that doesn't work for me, and it fails with following error:
从我在本文中可以阅读的内容来看,修改值的正确方法是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'>
Now, I'm confused
现在,我很困惑
What am I doing wrong?
我究竟做错了什么?
2 个解决方案
#1
3
Assuming IBM
is a pd.DataFrame
, IBM["PNL"]
is a pd.Series
. The []
(square brackets) calls the __getitem__
method and returns a series object. You then call the __getitem__
method on the series that was returned with IBM["PNL"][2]
, namely the [2]
part. That's fine for now, even if a bit confusing. The problem occurred when you tried to assign to it. IBM["PNL"][2] = 3
was telling pandas
to assign to the 2nd element of the pd.Series
IBM["PNL"]
which is a view of the "PNL"
column inside the IBM
dataframe... dizzy yet?
假设IBM是pd.DataFrame,IBM [“PNL”]是pd.Series。 [](方括号)调用__getitem__方法并返回一个系列对象。然后在IBM [“PNL”] [2]返回的系列上调用__getitem__方法,即[2]部分。现在这很好,即使有点令人困惑。尝试分配时出现问题。 IBM [“PNL”] [2] = 3告诉pandas分配给pd.Series IBM [“PNL”]的第二个元素,这是IBM数据框内“PNL”列的视图...晕了?
So the answer is to assign directly to the IBM
dataframe with loc
, iloc
, at
, iat
, or set_value
using proper indexers.
所以答案是使用适当的索引器直接使用loc,iloc,at,iat或set_value分配给IBM数据帧。
loc
Allows you to pass 1-D arrays as indexers. Arrays can be either slices (subsets) of the index or column, or they can be boolean arrays which are equal in length to the index or columns.
loc允许您将1-D数组作为索引器传递。数组可以是索引或列的切片(子集),也可以是布尔数组,其长度与索引或列相等。
Special Note: when a scalar indexer is passed, loc
can assign a new index or column value that didn't exist before.
特别注意:当传递标量索引器时,loc可以分配之前不存在的新索引或列值。
# described by @ayhan
IBM.loc[IBM.index[2], 'PNL'] = 3
iloc
Similar to loc
except with positions rather that index values. However, you cannot assign new columns or indices.
iloc类似于loc,除了位置而不是索引值。但是,您无法分配新列或索引。
# described by @ayhan
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
at
Works very similar to loc
for scalar indexers. Cannot operate on array indexers. Can! assign new indices and columns
在Works非常类似于标量索引器的loc。无法对阵列索引器进行操作。能够!分配新的索引和列
IBM.at[IBM.index[2], 'PNL'] = 3
iat
Works similarly to iloc
. Cannot work in array indexers. Cannot! assign new indices and columns.
iat与iloc类似。无法在数组索引器中工作。不能!分配新的索引和列。
IBM.iat[2, IBM.columns.get_loc('PNL')] = 3
set_value
Works very similar to loc
for scalar indexers. Cannot operate on array indexers. Can! assign new indices and columns
set_value与标量索引器的loc非常相似。无法对阵列索引器进行操作。能够!分配新的索引和列
IBM.set_value(IBM.index[2], 'PNL', 3)
set_value
with takable=True
Works similarly to iloc
. Cannot work in array indexers. Cannot! assign new indices and columns.
set_value with takable = True与iloc类似。无法在数组索引器中工作。不能!分配新的索引和列。
IBM.set_value(2, IBM.columns.get_loc('PNL'), 3, takable=True)
#2
4
Now that .ix
is deprecated, you have two alternatives:
既然.ix已弃用,您有两种选择:
IBM.loc[IBM.index[2], "PNL"] = 3
This is label based indexing. Since you need the label but you have the position, you use IBM.index[2]
to return the label. Or,
这是基于标签的索引。由于您需要标签,但您有位置,因此使用IBM.index [2]返回标签。要么,
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
This is position based indexing. To get the position of column PNL
, you use get_loc
.
这是基于位置的索引。要获取列PNL的位置,请使用get_loc。
The error was because you were trying to use position based indexing for rows but label based indexing for columns. .ix
was designed to handle such cases but it is deprecated. Here are details as noted by @jezrael.
该错误是因为您尝试对行使用基于位置的索引,但对列使用基于标签的索引。 .ix旨在处理此类情况但不推荐使用。以下是@jezrael所述的详细信息。
#1
3
Assuming IBM
is a pd.DataFrame
, IBM["PNL"]
is a pd.Series
. The []
(square brackets) calls the __getitem__
method and returns a series object. You then call the __getitem__
method on the series that was returned with IBM["PNL"][2]
, namely the [2]
part. That's fine for now, even if a bit confusing. The problem occurred when you tried to assign to it. IBM["PNL"][2] = 3
was telling pandas
to assign to the 2nd element of the pd.Series
IBM["PNL"]
which is a view of the "PNL"
column inside the IBM
dataframe... dizzy yet?
假设IBM是pd.DataFrame,IBM [“PNL”]是pd.Series。 [](方括号)调用__getitem__方法并返回一个系列对象。然后在IBM [“PNL”] [2]返回的系列上调用__getitem__方法,即[2]部分。现在这很好,即使有点令人困惑。尝试分配时出现问题。 IBM [“PNL”] [2] = 3告诉pandas分配给pd.Series IBM [“PNL”]的第二个元素,这是IBM数据框内“PNL”列的视图...晕了?
So the answer is to assign directly to the IBM
dataframe with loc
, iloc
, at
, iat
, or set_value
using proper indexers.
所以答案是使用适当的索引器直接使用loc,iloc,at,iat或set_value分配给IBM数据帧。
loc
Allows you to pass 1-D arrays as indexers. Arrays can be either slices (subsets) of the index or column, or they can be boolean arrays which are equal in length to the index or columns.
loc允许您将1-D数组作为索引器传递。数组可以是索引或列的切片(子集),也可以是布尔数组,其长度与索引或列相等。
Special Note: when a scalar indexer is passed, loc
can assign a new index or column value that didn't exist before.
特别注意:当传递标量索引器时,loc可以分配之前不存在的新索引或列值。
# described by @ayhan
IBM.loc[IBM.index[2], 'PNL'] = 3
iloc
Similar to loc
except with positions rather that index values. However, you cannot assign new columns or indices.
iloc类似于loc,除了位置而不是索引值。但是,您无法分配新列或索引。
# described by @ayhan
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
at
Works very similar to loc
for scalar indexers. Cannot operate on array indexers. Can! assign new indices and columns
在Works非常类似于标量索引器的loc。无法对阵列索引器进行操作。能够!分配新的索引和列
IBM.at[IBM.index[2], 'PNL'] = 3
iat
Works similarly to iloc
. Cannot work in array indexers. Cannot! assign new indices and columns.
iat与iloc类似。无法在数组索引器中工作。不能!分配新的索引和列。
IBM.iat[2, IBM.columns.get_loc('PNL')] = 3
set_value
Works very similar to loc
for scalar indexers. Cannot operate on array indexers. Can! assign new indices and columns
set_value与标量索引器的loc非常相似。无法对阵列索引器进行操作。能够!分配新的索引和列
IBM.set_value(IBM.index[2], 'PNL', 3)
set_value
with takable=True
Works similarly to iloc
. Cannot work in array indexers. Cannot! assign new indices and columns.
set_value with takable = True与iloc类似。无法在数组索引器中工作。不能!分配新的索引和列。
IBM.set_value(2, IBM.columns.get_loc('PNL'), 3, takable=True)
#2
4
Now that .ix
is deprecated, you have two alternatives:
既然.ix已弃用,您有两种选择:
IBM.loc[IBM.index[2], "PNL"] = 3
This is label based indexing. Since you need the label but you have the position, you use IBM.index[2]
to return the label. Or,
这是基于标签的索引。由于您需要标签,但您有位置,因此使用IBM.index [2]返回标签。要么,
IBM.iloc[2, IBM.columns.get_loc('PNL')] = 3
This is position based indexing. To get the position of column PNL
, you use get_loc
.
这是基于位置的索引。要获取列PNL的位置,请使用get_loc。
The error was because you were trying to use position based indexing for rows but label based indexing for columns. .ix
was designed to handle such cases but it is deprecated. Here are details as noted by @jezrael.
该错误是因为您尝试对行使用基于位置的索引,但对列使用基于标签的索引。 .ix旨在处理此类情况但不推荐使用。以下是@jezrael所述的详细信息。