/ Python And R Data science skills: 43 Python Pandas Series part 03

Monday 5 February 2018

43 Python Pandas Series part 03

43 pandas series tutorialpoint
In [2]:
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print (s)
Series([], dtype: float64)
In [3]:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print (s)
0    5
1    5
2    5
3    5
dtype: int64
In [7]:
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve multiple elements
print (s['f'])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   2565             try:
-> 2566                 return libts.get_value_box(s, key)
   2567             except IndexError:

pandas/_libs/tslib.pyx in pandas._libs.tslib.get_value_box()

pandas/_libs/tslib.pyx in pandas._libs.tslib.get_value_box()

TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-7-55924ad59d03> in <module>()
      3 
      4 #retrieve multiple elements
----> 5 print (s['f'])

~\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    621         key = com._apply_if_callable(key, self)
    622         try:
--> 623             result = self.index.get_value(self, key)
    624 
    625             if not is_scalar(result):

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   2572                     raise InvalidIndexError(key)
   2573                 else:
-> 2574                     raise e1
   2575             except Exception:  # pragma: no cover
   2576                 raise e1

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   2558         try:
   2559             return self._engine.get_value(s, k,
-> 2560                                           tz=getattr(series.dtype, 'tz', None))
   2561         except KeyError as e1:
   2562             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'f'

No comments:

Post a Comment