In [1]:
1 + 2
Out[1]:
3
In [2]:
3 * 4
10 * 10
Out[2]:
100
In [3]:
print(3 * 4)
print(10 * 10)
12
100
In [4]:
print(x)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-fc17d851ef81> in <module>()
----> 1 print(x)

NameError: name 'x' is not defined
In [ ]:
x = 300
In [5]:
import pandas as pd
In [6]:
from pandas import Series, DataFrame
In [7]:
nums_list = [100, 200, 300]
In [11]:
nums_series = Series(nums_list)
In [10]:
nums_list
Out[10]:
[100, 200, 300]
In [12]:
nums_series
Out[12]:
0    100
1    200
2    300
dtype: int64
In [13]:
s1 = Series([1, 2, 3])
In [14]:
s1
Out[14]:
0    1
1    2
2    3
dtype: int64
In [15]:
mylist1 = list(s1)
mylist1
Out[15]:
[1, 2, 3]
In [16]:
nums_list
Out[16]:
[100, 200, 300]
In [17]:
result = []
for num in nums_list:
    result.append(num + 1)
result
Out[17]:
[101, 201, 301]
In [18]:
nums_series
Out[18]:
0    100
1    200
2    300
dtype: int64
In [19]:
nums_series + 1
Out[19]:
0    101
1    201
2    301
dtype: int64
In [20]:
nums_series * 3
Out[20]:
0    300
1    600
2    900
dtype: int64
In [21]:
nums_list
Out[21]:
[100, 200, 300]
In [22]:
nums_list * 3
Out[22]:
[100, 200, 300, 100, 200, 300, 100, 200, 300]
In [23]:
[1, 2] + [3, 4]
Out[23]:
[1, 2, 3, 4]
In [24]:
Series([1, 2]) + Series([3, 4])
Out[24]:
0    4
1    6
dtype: int64
In [25]:
Series([1, 2, 5]) + Series([3, 4])
Out[25]:
0    4.0
1    6.0
2    NaN
dtype: float64
In [26]:
nums = Series([1, 9, 8, 2])
nums
Out[26]:
0    1
1    9
2    8
3    2
dtype: int64
In [27]:
nums > 5
Out[27]:
0    False
1     True
2     True
3    False
dtype: bool
In [28]:
B = nums > 5
In [29]:
B
Out[29]:
0    False
1     True
2     True
3    False
dtype: bool
In [30]:
mod2 = nums % 2
mod2
Out[30]:
0    1
1    1
2    0
3    0
dtype: int64
In [31]:
nums
Out[31]:
0    1
1    9
2    8
3    2
dtype: int64
In [32]:
nums
Out[32]:
0    1
1    9
2    8
3    2
dtype: int64
In [33]:
x = Series([2, 3, 4, 5])
nums % x
Out[33]:
0    1
1    0
2    0
3    2
dtype: int64
In [34]:
nums
Out[34]:
0    1
1    9
2    8
3    2
dtype: int64
In [35]:
mod2
Out[35]:
0    1
1    1
2    0
3    0
dtype: int64
In [37]:
nums[mod2 == 1]
Out[37]:
0    1
1    9
dtype: int64
In [38]:
nums1 = Series([100, 200, 300])
In [39]:
nums1
Out[39]:
0    100
1    200
2    300
dtype: int64
In [40]:
nums2 = Series([100, 200, 300], index=[2, 1, 0])
In [41]:
nums2
Out[41]:
2    100
1    200
0    300
dtype: int64
In [42]:
nums3 = Series([10, 20, 30])
In [43]:
nums3
Out[43]:
0    10
1    20
2    30
dtype: int64
In [44]:
nums1
Out[44]:
0    100
1    200
2    300
dtype: int64
In [45]:
nums3
Out[45]:
0    10
1    20
2    30
dtype: int64
In [46]:
nums1 + nums3
Out[46]:
0    110
1    220
2    330
dtype: int64
In [47]:
nums1
Out[47]:
0    100
1    200
2    300
dtype: int64
In [48]:
nums2
Out[48]:
2    100
1    200
0    300
dtype: int64
In [49]:
nums1 + nums2
Out[49]:
0    400
1    400
2    400
dtype: int64
In [50]:
nums2 = Series([10, 20, 30], index=[2, 1, 0])
nums2
Out[50]:
2    10
1    20
0    30
dtype: int64
In [51]:
nums1
Out[51]:
0    100
1    200
2    300
dtype: int64
In [52]:
nums1 + nums2
Out[52]:
0    130
1    220
2    310
dtype: int64
In [53]:
letters_list = ["A", "B", "C", "D"]
letters_series = Series(letters_list)
In [54]:
letters_list
Out[54]:
['A', 'B', 'C', 'D']
In [55]:
letters_series
Out[55]:
0    A
1    B
2    C
3    D
dtype: object
In [56]:
multi_type_series = Series([1, "A", True, 3.14])
multi_type_series
Out[56]:
0       1
1       A
2    True
3    3.14
dtype: object
In [57]:
letters_list
Out[57]:
['A', 'B', 'C', 'D']
In [58]:
letters_series
Out[58]:
0    A
1    B
2    C
3    D
dtype: object
In [59]:
letters_list[0]
Out[59]:
'A'
In [60]:
letters_series[0]
Out[60]:
'A'
In [61]:
letters_list[-1]
Out[61]:
'D'
In [62]:
letters_series[-1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-62-97b23244a061> in <module>()
----> 1 letters_series[-1]

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             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.Int64HashTable.get_item()

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

KeyError: -1
In [63]:
letters_list[:2]
Out[63]:
['A', 'B']
In [64]:
letters_series[:2]
Out[64]:
0    A
1    B
dtype: object
In [65]:
letters_list[2:]
Out[65]:
['C', 'D']
In [66]:
letters_series[2:]
Out[66]:
2    C
3    D
dtype: object
In [67]:
letters_list[:-1]
Out[67]:
['A', 'B', 'C']
In [68]:
letters_series[:-1]
Out[68]:
0    A
1    B
2    C
dtype: object
In [69]:
letters = letters_series
In [70]:
letters
Out[70]:
0    A
1    B
2    C
3    D
dtype: object
In [71]:
bool_series = Series([True, True, False, False])
In [72]:
bool_series
Out[72]:
0     True
1     True
2    False
3    False
dtype: bool
In [73]:
letters[bool_series]
Out[73]:
0    A
1    B
dtype: object
In [74]:
bool_series = Series([False, False, True, True])
In [75]:
letters[bool_series]
Out[75]:
2    C
3    D
dtype: object
In [76]:
letters
Out[76]:
0    A
1    B
2    C
3    D
dtype: object
In [77]:
bool_series = Series([True, False, True, False])
In [78]:
letters[bool_series]
Out[78]:
0    A
2    C
dtype: object
In [79]:
S = Series([1, 9, 2, 3, 8])
S
Out[79]:
0    1
1    9
2    2
3    3
4    8
dtype: int64
In [80]:
B = S > 5
B
Out[80]:
0    False
1     True
2    False
3    False
4     True
dtype: bool
In [81]:
S[B]
Out[81]:
1    9
4    8
dtype: int64
In [82]:
S
Out[82]:
0    1
1    9
2    2
3    3
4    8
dtype: int64
In [83]:
E = S % 2 == 0
In [84]:
E
Out[84]:
0    False
1    False
2     True
3    False
4     True
dtype: bool
In [85]:
S[E]
Out[85]:
2    2
4    8
dtype: int64
In [86]:
S[S % 2 == 0]
Out[86]:
2    2
4    8
dtype: int64
In [87]:
S[S % 2 == 1]
Out[87]:
0    1
1    9
3    3
dtype: int64
In [88]:
words = Series(["APPLE", "boy", "CAT", "dog"])
words
Out[88]:
0    APPLE
1      boy
2      CAT
3      dog
dtype: object
In [89]:
upper_words = words.str.upper()
upper_words
Out[89]:
0    APPLE
1      BOY
2      CAT
3      DOG
dtype: object
In [90]:
B = words == upper_words
In [91]:
B
Out[91]:
0     True
1    False
2     True
3    False
dtype: bool
In [92]:
words[B]
Out[92]:
0    APPLE
2      CAT
dtype: object
In [93]:
words[words == upper_words]
Out[93]:
0    APPLE
2      CAT
dtype: object
In [94]:
words[words == words.str.upper()]
Out[94]:
0    APPLE
2      CAT
dtype: object