In [3]:
import numpy as np

In [4]:
import pandas as pd

In [5]:
labels  = ["A","B","C"]

In [6]:
my_data = [10,20,30]

In [7]:
arr = np.array(my_data)
d  = {"A":10,"B":20,"C":30}

In [8]:
d


Out[8]:
{'A': 10, 'B': 20, 'C': 30}

In [9]:
pd.Series(my_data,labels)


Out[9]:
A    10
B    20
C    30
dtype: int64

In [11]:
pd.Series(arr,labels)


Out[11]:
A    10
B    20
C    30
dtype: int64

In [12]:
pd.Series(d)


Out[12]:
A    10
B    20
C    30
dtype: int64

In [13]:
pd.Series(labels,labels)


Out[13]:
A    A
B    B
C    C
dtype: object

In [16]:
pd.Series([1,2,4],["USA","INDIA","USSR"])


Out[16]:
USA      1
INDIA    2
USSR     4
dtype: int64

In [18]:
x = pd.Series([6,7,"India Is ..."],["India","USA","USSR"])

In [19]:
x["India"]


Out[19]:
6

In [20]:
x["USSR"] = "India is an ally"

In [21]:
x


Out[21]:
India                   6
USA                     7
USSR     India is an ally
dtype: object

In [24]:
np.randn(5,5)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-9f8043bad1be> in <module>()
----> 1 np.randn(5,5)

AttributeError: module 'numpy' has no attribute 'randn'

In [25]:
from numpy.random import randn

In [27]:
np.random.seed(101)

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

In [44]:
randn(5,4)
df


Out[44]:
W X Y Z new-co
A 0.975720 -0.388239 0.783316 -0.708954 -0.388239
B 0.586847 -1.621348 0.677535 0.026105 -1.621348
C -1.678284 0.333973 -0.532471 2.117727 0.333973
D 0.197524 2.302987 0.729024 -0.863091 2.302987
E 0.305632 0.243178 0.864165 -1.560931 0.243178

In [40]:
df["new-co"] = df["X"]

In [43]:
df.drop("new-co",axis=1)


Out[43]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [45]:
df.drop("new-co",axis=1,inplace=True)

In [46]:
df


Out[46]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [55]:
df[["W","Y","Z"]]


Out[55]:
W Y Z
A 0.975720 0.783316 -0.708954
B 0.586847 0.677535 0.026105
C -1.678284 -0.532471 2.117727
D 0.197524 0.729024 -0.863091
E 0.305632 0.864165 -1.560931

In [58]:
df["NEW"] = df["W"]

In [66]:
df.drop("NEW",axis=1,inplace=True)
df


Out[66]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [63]:
df


Out[63]:
W X Y Z NEW
A 0.975720 -0.388239 0.783316 -0.708954 0.975720
B 0.586847 -1.621348 0.677535 0.026105 0.586847
C -1.678284 0.333973 -0.532471 2.117727 -1.678284
D 0.197524 2.302987 0.729024 -0.863091 0.197524
E 0.305632 0.243178 0.864165 -1.560931 0.305632

In [68]:
df.loc["A"]


Out[68]:
W    0.975720
X   -0.388239
Y    0.783316
Z   -0.708954
Name: A, dtype: float64

In [70]:
df.loc["E"]


Out[70]:
W    0.305632
X    0.243178
Y    0.864165
Z   -1.560931
Name: E, dtype: float64

In [71]:
df.loc["D","Y"]


Out[71]:
0.72902373213544547

In [74]:
df.loc[["A","E"],["X","Y"]]


Out[74]:
X Y
A -0.388239 0.783316
E 0.243178 0.864165

In [75]:
df


Out[75]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [76]:
df["Y"]


Out[76]:
A    0.783316
B    0.677535
C   -0.532471
D    0.729024
E    0.864165
Name: Y, dtype: float64

In [77]:
df[df>0]


Out[77]:
W X Y Z
A 0.975720 NaN 0.783316 NaN
B 0.586847 NaN 0.677535 0.026105
C NaN 0.333973 NaN 2.117727
D 0.197524 2.302987 0.729024 NaN
E 0.305632 0.243178 0.864165 NaN

In [78]:
df["W"]


Out[78]:
A    0.975720
B    0.586847
C   -1.678284
D    0.197524
E    0.305632
Name: W, dtype: float64

In [79]:
df[["W","Y","Z"]]


Out[79]:
W Y Z
A 0.975720 0.783316 -0.708954
B 0.586847 0.677535 0.026105
C -1.678284 -0.532471 2.117727
D 0.197524 0.729024 -0.863091
E 0.305632 0.864165 -1.560931

In [80]:
my_list = ["X","Y","Z"]

In [81]:
df[my_list]


Out[81]:
X Y Z
A -0.388239 0.783316 -0.708954
B -1.621348 0.677535 0.026105
C 0.333973 -0.532471 2.117727
D 2.302987 0.729024 -0.863091
E 0.243178 0.864165 -1.560931

In [82]:
df["New-Col"] = df["X"] + df["Y"]

In [83]:
df


Out[83]:
W X Y Z New-Col
A 0.975720 -0.388239 0.783316 -0.708954 0.395077
B 0.586847 -1.621348 0.677535 0.026105 -0.943813
C -1.678284 0.333973 -0.532471 2.117727 -0.198497
D 0.197524 2.302987 0.729024 -0.863091 3.032011
E 0.305632 0.243178 0.864165 -1.560931 1.107342

In [84]:
df.drop("New-Col",axis=1,inplace=True)

In [85]:
df


Out[85]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [86]:
df.loc["A"]


Out[86]:
W    0.975720
X   -0.388239
Y    0.783316
Z   -0.708954
Name: A, dtype: float64

In [87]:
my_idf = ["A","B","C"]

In [88]:
df.loc[my_idf]


Out[88]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727

In [91]:
df.loc[my_idf,my_list]


Out[91]:
X Y Z
A -0.388239 0.783316 -0.708954
B -1.621348 0.677535 0.026105
C 0.333973 -0.532471 2.117727

In [94]:
x = df.index

In [95]:
x


Out[95]:
Index(['A', 'B', 'C', 'D', 'E'], dtype='object')

In [96]:
df.loc[x]


Out[96]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [97]:
df.columns


Out[97]:
Index(['W', 'X', 'Y', 'Z'], dtype='object')

In [98]:
cols = df.columns

In [99]:
df.loc[x,cols]


Out[99]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [101]:
df["W"]>.5


Out[101]:
A     True
B     True
C    False
D    False
E    False
Name: W, dtype: bool

In [106]:
df[["X","Y"]]


Out[106]:
X Y
A -0.388239 0.783316
B -1.621348 0.677535
C 0.333973 -0.532471
D 2.302987 0.729024
E 0.243178 0.864165

In [119]:
df[df["W"]>0][["W","X"]]


Out[119]:
W X
A 0.975720 -0.388239
B 0.586847 -1.621348
D 0.197524 2.302987
E 0.305632 0.243178

In [118]:
df[df['W']>0][['W','X']]


Out[118]:
W X
A 0.975720 -0.388239
B 0.586847 -1.621348
D 0.197524 2.302987
E 0.305632 0.243178

In [121]:
df[df["Y"]>0][["X","Y"]]


Out[121]:
X Y
A -0.388239 0.783316
B -1.621348 0.677535
D 2.302987 0.729024
E 0.243178 0.864165

In [123]:
boolx = df["W"]>0

In [124]:
boolx


Out[124]:
A     True
B     True
C    False
D     True
E     True
Name: W, dtype: bool

df[boolx]


In [126]:
my_cols = ["X","Y"]

In [127]:
my_cols


Out[127]:
['X', 'Y']

In [129]:
res = df[boolx]

In [130]:
res[my_cols]


Out[130]:
X Y
A -0.388239 0.783316
B -1.621348 0.677535
D 2.302987 0.729024
E 0.243178 0.864165

In [141]:
df[(df["W"]>0) & (df["X"]>1)][["W","X","Y"]]


Out[141]:
W X Y
D 0.197524 2.302987 0.729024

In [138]:
df


Out[138]:
W X Y Z
A 0.975720 -0.388239 0.783316 -0.708954
B 0.586847 -1.621348 0.677535 0.026105
C -1.678284 0.333973 -0.532471 2.117727
D 0.197524 2.302987 0.729024 -0.863091
E 0.305632 0.243178 0.864165 -1.560931

In [142]:
time.clock()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-142-d0ea78653c97> in <module>()
----> 1 time.clock()

NameError: name 'time' is not defined

In [143]:
import time

In [145]:
x = time.time()

In [146]:
type(x)


Out[146]:
float

In [147]:
x


Out[147]:
1480926894.865967

In [153]:
x = pd.date_range('07:00:00', periods=10, freq='30min')

In [168]:
x
type(x)


Out[168]:
pandas.tseries.index.DatetimeIndex

In [162]:
pd.date_range('07:00:00', periods=10, freq='15min')


Out[162]:
DatetimeIndex(['2016-12-05 07:00:00', '2016-12-05 07:15:00',
               '2016-12-05 07:30:00', '2016-12-05 07:45:00',
               '2016-12-05 08:00:00', '2016-12-05 08:15:00',
               '2016-12-05 08:30:00', '2016-12-05 08:45:00',
               '2016-12-05 09:00:00', '2016-12-05 09:15:00'],
              dtype='datetime64[ns]', freq='15T')

In [165]:
datetime  = 6

In [166]:
datetime


Out[166]:
6

In [167]:
type(datetime)


Out[167]:
int

In [171]:



---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-171-490ec51554aa> in <module>()
----> 1 datetime.time

AttributeError: 'int' object has no attribute 'time'

In [170]:
from datetime import timedelta

In [172]:
def add_nums(x,y):
    return x+y
add_nums(2,2)


Out[172]:
4

In [174]:
def add_numbers(x,y,z):
    return x+y+z

add_numbers(1,2,3)


Out[174]:
6

In [179]:
def do_math(a,b,kind)
  if (kind == 'add'):
    return a+b
  else:
    return a-b


  File "<ipython-input-179-a80a9d766e4f>", line 1
    def do_math(a,b,kind)
                         ^
SyntaxError: invalid syntax

In [180]:
datetime.datetime.strptime('03:55', '%H:%M').time()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-180-e3dc90cff41b> in <module>()
----> 1 datetime.datetime.strptime('03:55', '%H:%M').time()

AttributeError: 'int' object has no attribute 'datetime'

In [181]:
datetime.time(*map(int, '03:55'.split(':')))
datetime.time(3, 55)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-181-29f1184566d6> in <module>()
----> 1 datetime.time(*map(int, '03:55'.split(':')))
      2 datetime.time(3, 55)

AttributeError: 'int' object has no attribute 'time'

In [183]:
lst = [1,2]

In [182]:
lst2 = [3,4]

In [184]:
z = lst + lst2

In [185]:
z


Out[185]:
[1, 2, 3, 4]

In [186]:
lst*3


Out[186]:
[1, 2, 1, 2, 1, 2]

In [189]:
lst[0]*3


Out[189]:
3

In [188]:
lst[1]*3


Out[188]:
6

In [190]:
1 in lst


Out[190]:
True

In [191]:
6 in lst


Out[191]:
False

In [ ]: