import numpy as np
A = np.array([[1, 2, 3],
[2, 3, 4]])
u = np.array([10, 20, 30])
A + u
array([[11, 22, 33], [12, 23, 34]])
A * u
array([[ 10, 40, 90], [ 20, 60, 120]])
T = np.array([[[2, 3],
[4, 1]],
[[1, 10],
[5, 9]]])
T.shape
(2, 2, 2)
T + np.array([100, 200])
array([[[102, 203], [104, 201]], [[101, 210], [105, 209]]])
T.shape
(2, 2, 2)
T + np.array([[100, 200]])
array([[[102, 203], [104, 201]], [[101, 210], [105, 209]]])
np.array([[100, 200]]).shape
(1, 2)
T + np.array([[100], [200]])
array([[[102, 103], [204, 201]], [[101, 110], [205, 209]]])
np.array([[100], [200]]).shape
(2, 1)
np.array([[1, 2, 3]]) + np.array([[10], [20]])
array([[11, 12, 13], [21, 22, 23]])
np.array([[1, 2, 3]]) * np.array([[10], [20]])
array([[10, 20, 30], [20, 40, 60]])
np.array([[10], [20]]) @ np.array([[1, 2, 3]])
array([[10, 20, 30], [20, 40, 60]])
np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3)
array([[1, 2, 3], [4, 5, 6]])
np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 3)
array([[1, 2, 3], [4, 5, 6]])
np.array([1, 2, 3, 4, 5, 6]).reshape(3, -1)
array([[1, 2], [3, 4], [5, 6]])
np.array([1, 2, 3, 4, 5, 6]).T
array([1, 2, 3, 4, 5, 6])
np.array([1, 2, 3, 4, 5, 6]).reshape(1, -1).T
array([[1], [2], [3], [4], [5], [6]])
np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1)
array([[1], [2], [3], [4], [5], [6]])
np.array([1, 2, 3, 4, 5, 6]).reshape(2, 1, 3)
array([[[1, 2, 3]], [[4, 5, 6]]])
x = np.array([1, 2, 3, 4, 5, 6])
x
array([1, 2, 3, 4, 5, 6])
x[x % 2 == 0] = 0
x
array([1, 0, 3, 0, 5, 0])
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([10, 20, 30, 40, 50, 60])
np.where(x % 2 == 0, 100, x)
array([ 1, 100, 3, 100, 5, 100])
np.linalg.solve(np.array([[3, 2],
[1, 4]]),
np.array([10, 20]))
array([-1.13489465e-16, 5.00000000e+00])
np.random.multivariate_normal(np.array([0, 0]),
np.array([[1, 0.7],
[0.7, 1]]), size=5).T
array([[ 0.852054 , -0.91145745, -2.07958499, -0.84560849, 0.49968892], [-0.20504889, -0.54984662, -1.4427939 , -0.21885378, -0.17271844]])
x, y = np.random.multivariate_normal(np.array([0, 0]),
np.array([[1, 0.9],
[0.9, 1]]), size=100).T
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(x, y, 'o')
[<matplotlib.lines.Line2D at 0x110c66fd0>]
import pandas as pd
s = pd.Series([10, 20, 30])
s
0 10 1 20 2 30 dtype: int64
s = pd.Series([10, 20, 30], index=['Alice', 'Bob', 'Claudia'])
s
Alice 10 Bob 20 Claudia 30 dtype: int64
s['Alice']
10
s[0]
10
s[1:]
Bob 20 Claudia 30 dtype: int64
u = pd.Series([30, 10, 40], index=['Angela', 'Alice', 'Claudia'])
s
Alice 10 Bob 20 Claudia 30 dtype: int64
u
Angela 30 Alice 10 Claudia 40 dtype: int64
u + s
Alice 20.0 Angela NaN Bob NaN Claudia 70.0 dtype: float64
nan = float("NaN")
x = nan
if x == nan:
print("x is NaN")
float("NaN") == float("NaN")
False
x = nan
if x != x and isinstance(x, float):
print("x is NaN")
x is NaN
class MyNaN(object):
def __eq__(self, other):
return False
x = MyNaN()
y = MyNaN()
x == y
False
None
None == float("NaN")
False
None == None
True
pd.isna(u + s).sum()
2
s
Alice 10 Bob 20 Claudia 30 dtype: int64
s.mean()
20.0
s.sum()
60
s
Alice 10 Bob 20 Claudia 30 dtype: int64
s['Alice']
10
s[0]
10
numbers = pd.Series([10, 20, 30, 40], index=[3, 2, 0, 1])
numbers
3 10 2 20 0 30 1 40 dtype: int64
numbers[0]
30
numbers.iloc[0]
10
numbers.loc[0]
30
s
Alice 10 Bob 20 Claudia 30 dtype: int64
s = pd.Series([10, 20, 30, 40, 50], index=['Alice', 'Bob', 'Claudia',
'Daniel', 'Mark'])
s
Alice 10 Bob 20 Claudia 30 Daniel 40 Mark 50 dtype: int64
s[1:3]
Bob 20 Claudia 30 dtype: int64
s['Bob':'Daniel']
Bob 20 Claudia 30 Daniel 40 dtype: int64
s[:2]
Alice 10 Bob 20 dtype: int64
s
Alice 10 Bob 20 Claudia 30 Daniel 40 Mark 50 dtype: int64
s['Anna':'Dmitry']
Bob 20 Claudia 30 Daniel 40 dtype: int64
s = pd.Series([10, 20, 30, 40, 50], index=['Bob', 'Claudia',
'Daniel','Alice', 'Mark'])
s
Bob 10 Claudia 20 Daniel 30 Alice 40 Mark 50 dtype: int64
s['Anna':'Dmitry']
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind) 5166 try: -> 5167 return self._searchsorted_monotonic(label, side) 5168 except ValueError: /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in _searchsorted_monotonic(self, label, side) 5127 -> 5128 raise ValueError("index must be monotonic increasing or decreasing") 5129 ValueError: index must be monotonic increasing or decreasing During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-145-3e6e18e5b7c2> in <module>() ----> 1 s['Anna':'Dmitry'] /usr/local/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key) 1111 key = check_bool_indexer(self.index, key) 1112 -> 1113 return self._get_with(key) 1114 1115 def _get_with(self, key): /usr/local/lib/python3.7/site-packages/pandas/core/series.py in _get_with(self, key) 1116 # other: fancy integer or otherwise 1117 if isinstance(key, slice): -> 1118 indexer = self.index._convert_slice_indexer(key, kind="getitem") 1119 return self._get_values(indexer) 1120 elif isinstance(key, ABCDataFrame): /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in _convert_slice_indexer(self, key, kind) 3214 else: 3215 try: -> 3216 indexer = self.slice_indexer(start, stop, step, kind=kind) 3217 except Exception: 3218 if is_index_slice: /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in slice_indexer(self, start, end, step, kind) 5032 slice(1, 3) 5033 """ -> 5034 start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind) 5035 5036 # return a slice /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in slice_locs(self, start, end, step, kind) 5246 start_slice = None 5247 if start is not None: -> 5248 start_slice = self.get_slice_bound(start, "left", kind) 5249 if start_slice is None: 5250 start_slice = 0 /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind) 5168 except ValueError: 5169 # raise the original KeyError -> 5170 raise err 5171 5172 if isinstance(slc, np.ndarray): /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind) 5162 # we need to look up the label 5163 try: -> 5164 slc = self.get_loc(label) 5165 except KeyError as err: 5166 try: /usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2897 return self._engine.get_loc(key) 2898 except KeyError: -> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2901 if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() 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: 'Anna'
s['Daniel':'Mark']
Daniel 30 Alice 40 Mark 50 dtype: int64
uuu = pd.Series([10, 20, 30, 40, 50], index=['Bob', 'Bob',
'Bob','Bob', 'Mark'])
uuu['Bob']
Bob 10 Bob 20 Bob 30 Bob 40 dtype: int64
uuu['Mark']
50
uuu['Bob':'Mark']
Bob 10 Bob 20 Bob 30 Bob 40 Mark 50 dtype: int64
uuu + pd.Series([100, 200, 300, 400],
index=['Bob', 'Bob', 'Mark', 'Mark'])
Bob 110 Bob 210 Bob 120 Bob 220 Bob 130 Bob 230 Bob 140 Bob 240 Mark 350 Mark 450 dtype: int64
s
Bob 10 Claudia 20 Daniel 30 Alice 40 Mark 50 dtype: int64
s
Bob 10 Claudia 20 Daniel 30 Alice 40 Mark 50 dtype: int64
s[s > 20]
Daniel 30 Alice 40 Mark 50 dtype: int64
s > 20
Bob False Claudia False Daniel True Alice True Mark True dtype: bool
b = pd.Series([0, 'hello'])
b.replace({'hello': np.nan})
0 0.0 1 NaN dtype: float64
pd.Series([float("NaN"), 1]).replace({float("NaN"): 100})
0 100.0 1 1.0 dtype: float64
pd.Series([float("NaN"), 1]).fillna(100)
0 100.0 1 1.0 dtype: float64
pd.Series([None, 1]).fillna(100)
0 100.0 1 1.0 dtype: float64
pd.Series([1, None, 2, 3, None, 4, None]).fillna(method='backfill')
0 1.0 1 2.0 2 2.0 3 3.0 4 4.0 5 4.0 6 NaN dtype: float64
pd.Series([1, None, 2, 3, None, 4]).fillna(method='ffill')
0 1.0 1 1.0 2 2.0 3 3.0 4 3.0 5 4.0 dtype: float64
pd.Series([1, None, 2, 3, None, None, 4]).interpolate(
method='quadratic')
0 1.000000 1 1.300813 2 2.000000 3 3.000000 4 3.715447 5 4.048780 6 4.000000 dtype: float64