Вы находитесь на странице: 1из 26

Intro day 1Last Checkpoint: Last Friday at 2:00 PM(autosaved) Logout

Python 3
Trusted

 File
 Edit
 View
 Insert
 Cell
 Kernel
 Widgets
 Help

Run

In [1]:

s = "this is good"
t = True
a=7
b=6
f = 9.7
print("hello sum", a+b)

hello sum 13
In [3]:

s = "this is good"
t = True
a=7
b=6
f = 9.7
print("hello sum", a+b)
d=4

hello sum 13
In [ ]:

In [4]:

s = "this is good"
t = True
a=7
b=6
f = 9.7
#print("hello sum", a+b)

In [5]:

if a > 10:
print("ok")
else:
print("not ok")
not ok
In [6]:

a = int(input("enter the number"))


if a == 1:
print("ok")
elif a==2:
print("ok2")
elif a==3:
print("ok3")
else:
print("not ok")
enter the number2
ok2
In [8]:

for i in range(1,4):
for j in range(1,4):
print(i)
1
1
1
2
2
2
3
3
3
In [11]:

for i in range(1,4):
for j in range(1,4):
print(i, end = "i=2")
1i=21i=21i=22i=22i=22i=23i=23i=23i=2
In [12]:

for i in range(1,4):
for j in range(1,4):
print(i, end = "")
111222333
In [13]:

for i in range(1,4):
for j in range(1,4):
print(i, end = "")
print("\n")
111

222

333

In [14]:

for i in range(1,4):
print("\n")
for j in range(1,4):
print(i, end = "")

111

222

333
In [15]:

s = lambda x: x*x
print(s(5))
25
In [16]:
s = lambda x,y: x+y
print(s(2,3))
5
In [17]:

try:
a=9
b=0
print("the div is", a/b)

except Exception as e:
print("Error!", e)
Error! division by zero
In [18]:

try:
a=9
b=0
print("the div is", b/a)

except Exception as e:
print("Error!", e)
the div is 0.0
In [9]:

import numpy as np
In [2]:
l = [2,3,5,6]
np.array(l)
Out[2]:
array([2, 3, 5, 6])
In [3]:

my_matrix =[[1,2,3],[4,5,6], [7,8,9]]


my_matrix
Out[3]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [4]:

np.array(my_matrix)
Out[4]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [9]:

np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
Out[9]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
In [12]:
np.arange(0,10)
Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [7]:

np.zeros(4)
Out[7]:
array([0., 0., 0., 0.])
In [8]:

np.ones(3)
Out[8]:
array([1., 1., 1.])
In [10]:

np.zeros(5,5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-0be2ed1d2c69> in <module>
----> 1 np.zeros(5,5)

TypeError: data type not understood

In [11]:
np.zeros((5,5))
Out[11]:
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
In [12]:

np.linspace(0,10,4)
Out[12]:
array([ 0. , 3.33333333, 6.66666667, 10. ])
In [13]:

np.linspace(0.10,6)
Out[13]:
array([0.1 , 0.22040816, 0.34081633, 0.46122449, 0.58163265,
0.70204082, 0.82244898, 0.94285714, 1.06326531, 1.18367347,
1.30408163, 1.4244898 , 1.54489796, 1.66530612, 1.78571429,
1.90612245, 2.02653061, 2.14693878, 2.26734694, 2.3877551 ,
2.50816327, 2.62857143, 2.74897959, 2.86938776, 2.98979592,
3.11020408, 3.23061224, 3.35102041, 3.47142857, 3.59183673,
3.7122449 , 3.83265306, 3.95306122, 4.07346939, 4.19387755,
4.31428571, 4.43469388, 4.55510204, 4.6755102 , 4.79591837,
4.91632653, 5.03673469, 5.15714286, 5.27755102, 5.39795918,
5.51836735, 5.63877551, 5.75918367, 5.87959184, 6. ])
In [14]:
np.linspace(0,10,6)
Out[14]:
array([ 0., 2., 4., 6., 8., 10.])
In [3]:

import random
In [7]:

random.randint(1,10)
Out[7]:
4
In [8]:

np.random.rand(4)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-63d0acad6bb4> in <module>
----> 1 np.random.rand(4)

NameError: name 'np' is not defined

In [10]:

np.random.rand(7)
Out[10]:
array([0.84026051, 0.68889086, 0.52492321, 0.43262344, 0.06019164,
0.22666551, 0.75850704])
In [23]:
np.random.rand(5,5)
Out[23]:
array([[0.63319009, 0.05468459, 0.86719802, 0.57467522, 0.79109165],
[0.73134838, 0.0800012 , 0.48351881, 0.90437749, 0.83405371],
[0.35944379, 0.19996895, 0.49880596, 0.54937713, 0.33440026],
[0.6628148 , 0.1358355 , 0.02842624, 0.20352366, 0.0729804 ],
[0.97083834, 0.62168946, 0.86759173, 0.23519234, 0.58733608]])
In [24]:

np.random.randn(5,5)
Out[24]:
array([[ 1.04926831, 1.39542519, 0.86472576, 0.06007758, -0.54405796],
[-0.45113187, -0.79282517, -0.14487855, -0.81817126, -0.57137778],
[-0.25792585, 0.03132514, 0.83200862, 0.53485948, -2.15863243],
[ 1.93795039, 1.00533897, -0.77763965, 1.69860581, -1.17202323],
[-0.23895415, 1.07441575, 0.17345031, 1.62337034, 0.63651691]])
In [29]:

np.random.randint(1,1999)
Out[29]:
1255
In [30]:

np.arange(12).reshape(3,4)
Out[30]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [31]:

np.arange(12).reshape(4,1) --> tried to convert back into 1 Dimension


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-54e7913c3a22> in <module>
----> 1 np.arange(12).reshape(4,1)

ValueError: cannot reshape array of size 12 into shape (4,1)

In [32]:

np.arange(12).reshape(1,4) --> tried to convert back into 1 Dimension. Check down


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-1b37338500ff> in <module>
----> 1 np.arange(12).reshape(1,4)

ValueError: cannot reshape array of size 12 into shape (1,4)

In [33]:

d=np.arange(12).reshape(3,4)
In [34]:

d
Out[34]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [35]:

d.reshape(12)
Out[35]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
In [ ]:

In [37]:

d.shape --> tells you about number of rows and columns


Out[37]:
(3, 4)
In [38]:

arr = np.arange(0,11)
In [39]:

arr
Out[39]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
In [40]:
arr[6]
Out[40]:
6
In [41]:

arr[3:7]
Out[41]:
array([3, 4, 5, 6])
In [42]:

arr[:7]
Out[42]:
array([0, 1, 2, 3, 4, 5, 6])
In [43]:

arr[7:]
Out[43]:
array([ 7, 8, 9, 10])
In [46]:

arr[:7]=100 # upto index 7 make all elements 100


In [47]:
arr
Out[47]:
array([100, 100, 100, 100, 100, 100, 100, 7, 8, 9, 10])
In [3]:

import numpy as np
In [4]:

t = np.array([[3,4,5],[3,5,6],[3,5,67]])
In [23]:

t
Out[23]:
array([[ 3, 4, 5],
[ 3, 5, 6],
[ 3, 5, 67]])
In [25]:

t[2][2] #to get 67


Out[25]:
67
In [7]:
t[1][1 ] #to get 5 of 2nd row, 2nd column
Out[7]:
5
In [26]:

t[1:2,1:3] # row 1 too 2 excluding 2, column 1 to 3 exclusing 3;(start counting from 0 --> [0,1,2,3) last point is
excluded)
Out[26]:
array([[5, 6]])
In [4]:

l = [40.5, 45.5, 44.5 ,48 , 51 ,63 ,71 , 37.5, 38]


c=np.array(l)
In [5]:

c>[45] # to get greater than 45


Out[5]:
array([False, True, False, True, True, True, True, False, False])
In [6]:

c[c>45] # to get numerical values greater than 45


Out[6]:
array([45.5, 48. , 51. , 63. , 71. ])
In [16]:
a = np.arange(1,10)
In [17]:

a
Out[17]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [18]:

a+a
Out[18]:
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
In [19]:

b=np.arange(1,6)
In [20]:

a+b error since number of elements are not same.


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-ca730b97bf8a> in <module>
----> 1 a+b

ValueError: operands could not be broadcast together with shapes (9,) (5,)
In [27]:

import pandas as pd
In [38]:

labels = ['a', 'b', 'c']


In [29]:

my_list = [10,20,30]
In [30]:

arr=np.array([10,20,30])
In [32]:

pd.Series(data=my_list)
Out[32]:
0 10
1 20
2 30
dtype: int64
In [33]:
pd.Series(data=my_list,index = [1,2,3])
Out[33]:
1 10
2 20
3 30
dtype: int64
In [39]:

pd.Series(data=labels)
Out[39]:
0 a
1 b
2 c
dtype: object
In [40]:

pd.Series(data=arr)
Out[40]:
0 10
1 20
2 30
dtype: int32
In [36]:

d={'a':10,'b':20,'c':30}
In [41]:
pd.Series(data=d)
Out[41]:
a 10
b 20
c 30
dtype: int64
In [42]:

pd.Series(d)
Out[42]:
a 10
b 20
c 30
dtype: int64
In [43]:

pd.Series(arr,labels)
Out[43]:
a 10
b 20
c 30
dtype: int32
In [44]:

from numpy.random import randn


In [47]:
df = pd.DataFrame(randn(5,4), index = ['A', 'B', 'C', 'D', 'E'], columns=['W','X','Y','Z']) #without using split
In [49]:

df
Out[49]:

W X Y Z

A 0.602311 1.241096 0.262659 1.505764

B 0.891269 1.125875 -1.158768 -0.197734

C 1.213310 0.813816 0.533240 0.898456

D 1.149953 0.185008 -1.231521 -0.657351

E 0.886219 -1.990093 0.622133 -0.556576

In [50]:

'A B C D E'.split() #use of split


Out[50]:
['A', 'B', 'C', 'D', 'E']
In [52]:

df1 = pd.DataFrame(randn(5,4), index = 'A B C D E'.split(), columns='W X Y Z'.split()) #using split


In [53]:
df1
Out[53]:

W X Y Z

A 1.242723 -0.078928 -0.240874 1.508841

B -0.723014 0.290020 0.956740 -0.004253

C 0.325441 1.360848 2.128307 -1.987471

D -0.459777 0.600544 -1.193286 -0.023815

E -0.403849 0.065635 0.631906 0.146097

In [54]:

df['W']
Out[54]:
A -0.482512
B 0.526326
C -0.573692
D -2.121619
E -1.287729
Name: W, dtype: float64
In [56]:

df[['W','Z']]
Out[56]:
W Z

A -0.482512 -0.071771

B 0.526326 -0.723881

C -0.573692 1.457442

D -2.121619 -0.340125

E -1.287729 1.417590

In [58]:

df.W #not recommended if strings or other characters are included


Out[58]:
A -0.482512
B 0.526326
C -0.573692
D -2.121619
E -1.287729
Name: W, dtype: float64
In [59]:

df['new'] = df['W'] + df['Y'] #introducing a new column


In [60]:

df
Out[60]:
W X Y Z new

A -0.482512 0.952703 1.070455 -0.071771 0.587943

B 0.526326 -0.677332 0.109892 -0.723881 0.636217

C -0.573692 -0.758485 0.909044 1.457442 0.335352

D -2.121619 -2.599208 0.638866 -0.340125 -1.482753

E -1.287729 0.192356 0.523549 1.417590 -0.764180

In [61]:

df.drop('new', axis=1) #removing a column. axis = 1 means delete a column. axis = 0 means delete a row
Out[61]:

W X Y Z

A -0.482512 0.952703 1.070455 -0.071771

B 0.526326 -0.677332 0.109892 -0.723881

C -0.573692 -0.758485 0.909044 1.457442

D -2.121619 -2.599208 0.638866 -0.340125

E -1.287729 0.192356 0.523549 1.417590

In [63]:

df.drop('A', axis=0) #deleting a row


Out[63]:
W X Y Z new

B 0.526326 -0.677332 0.109892 -0.723881 0.636217

C -0.573692 -0.758485 0.909044 1.457442 0.335352

D -2.121619 -2.599208 0.638866 -0.340125 -1.482753

E -1.287729 0.192356 0.523549 1.417590 -0.764180

In [64]:

df # see in original no change. Hence use 'inplace = True' for permanent change
Out[64]:

W X Y Z new

A -0.482512 0.952703 1.070455 -0.071771 0.587943

B 0.526326 -0.677332 0.109892 -0.723881 0.636217

C -0.573692 -0.758485 0.909044 1.457442 0.335352

D -2.121619 -2.599208 0.638866 -0.340125 -1.482753

E -1.287729 0.192356 0.523549 1.417590 -0.764180

In [65]:

df.drop('A', axis=0, inplace = True)


In [66]:
df
Out[66]:

W X Y Z new

B 0.526326 -0.677332 0.109892 -0.723881 0.636217

C -0.573692 -0.758485 0.909044 1.457442 0.335352

D -2.121619 -2.599208 0.638866 -0.340125 -1.482753

E -1.287729 0.192356 0.523549 1.417590 -0.764180

In [67]:

df.loc['B'] # to select row B


Out[67]:
W 0.526326
X -0.677332
Y 0.109892
Z -0.723881
new 0.636217
Name: B, dtype: float64
In [68]:

df.iloc[0] #i = index. iloc index location


Out[68]:
W 0.526326
X -0.677332
Y 0.109892
Z -0.723881
new 0.636217
Name: B, dtype: float64
In [69]:

df.loc['B', 'Y']
Out[69]:
0.10989162475388488
In [72]:

df.loc['B C, 'W Y']


File "<ipython-input-72-79f92a938966>", line 1
df.loc['B C, 'W Y']
^
SyntaxError: invalid syntax

In [ ]:

Вам также может понравиться