dataframe之df.loc、df.iloc、df.ix

import pandas as pd
import numpy as np
data = pd.Series(np.arange(10),index = [1,2,3,4,5,6,7,8,9,10])
data
1     0
2     1
3     2
4     3
5     4
6     5
7     6
8     7
9     8
10    9
dtype: int32
data[:3]
1    0
2    1
3    2
dtype: int32
data.loc[:3]
1    0
2    1
3    2
dtype: int32
data.iloc[:3]
1    0
2    1
3    2
dtype: int32
data.ix[:3]
c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.





1    0
2    1
3    2
dtype: int32
import pandas as pd
import numpy as np
data = pd.Series(np.arange(10),index = [1,2,11,4,5,6,7,8,9,10])
data
1     0
2     1
11    2
4     3
5     4
6     5
7     6
8     7
9     8
10    9
dtype: int32
data.loc[:3]
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   3483             try:
-> 3484                 return self._searchsorted_monotonic(label, side)
   3485             except ValueError:


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in _searchsorted_monotonic(self, label, side)
   3442 
-> 3443         raise ValueError('index must be monotonic increasing or decreasing')
   3444 


ValueError: index must be monotonic increasing or decreasing


During handling of the above exception, another exception occurred:


KeyError                                  Traceback (most recent call last)

 in ()
----> 1 data.loc[:3]


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1326         else:
   1327             key = com._apply_if_callable(key, self.obj)
-> 1328             return self._getitem_axis(key, axis=0)
   1329 
   1330     def _is_scalar_access(self, key):


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1504         if isinstance(key, slice):
   1505             self._has_valid_type(key, axis)
-> 1506             return self._get_slice_axis(key, axis=axis)
   1507         elif is_bool_indexer(key):
   1508             return self._getbool_axis(key, axis=axis)


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexing.py in _get_slice_axis(self, slice_obj, axis)
   1354         labels = obj._get_axis(axis)
   1355         indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop,
-> 1356                                        slice_obj.step, kind=self.name)
   1357 
   1358         if isinstance(indexer, slice):


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in slice_indexer(self, start, end, step, kind)
   3348         """
   3349         start_slice, end_slice = self.slice_locs(start, end, step=step,
-> 3350                                                  kind=kind)
   3351 
   3352         # return a slice


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in slice_locs(self, start, end, step, kind)
   3542         end_slice = None
   3543         if end is not None:
-> 3544             end_slice = self.get_slice_bound(end, 'right', kind)
   3545         if end_slice is None:
   3546             end_slice = len(self)


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   3485             except ValueError:
   3486                 # raise the original KeyError
-> 3487                 raise err
   3488 
   3489         if isinstance(slc, np.ndarray):


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
   3479         # we need to look up the label
   3480         try:
-> 3481             slc = self._get_loc_only_exact_matches(label)
   3482         except KeyError as err:
   3483             try:


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in _get_loc_only_exact_matches(self, key)
   3448         get_slice_bound.
   3449         """
-> 3450         return self.get_loc(key)
   3451 
   3452     def get_slice_bound(self, label, side, kind):


c:\users\lulinlin\appdata\local\programs\python\python35\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2442                 return self._engine.get_loc(key)
   2443             except KeyError:
-> 2444                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2445 
   2446         indexer = self.get_indexer([key], method=method, tolerance=tolerance)


pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)()


pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)()


pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:14031)()


pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:13975)()


KeyError: 3
data.iloc[:3]
1     0
2     1
11    2
dtype: int32

解析:

loc 在index的标签上进行索引,范围包括start和end。

iloc 在index的位置上进行索引,不包括end。

ix 先在index的标签上索引,索引不到就在index的位置上索引(如果index非全整数),不包括end。(已弃用)

修改源数据的时候建议使用df.loc或者df.iloc,不然会出现SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

你可能感兴趣的:(Python)