import pandas as pd
In [4]:
ser = pd.Series([100, 200, 300, 400, 500],[ 'tom','bob','nancy','dan', 'eric'])

ser
Out[4]:
tom      100
bob      200
nancy    300
dan      400
eric     500
dtype: int64
In [5]:
ser = pd.Series([100, 'foo', 300, 400, 500],[ 'tom','bob','nancy','dan', 'eric'])

ser
Out[5]:
tom      100
bob      foo
nancy    300
dan      400
eric     500
dtype: object
In [6]:
ser.index
Out[6]:
Index(['tom', 'bob', 'nancy', 'dan', 'eric'], dtype='object')
In [7]:
ser['nancy'] , ser.loc['nancy']
Out[7]:
(300, 300)
In [8]:
ser[['nancy', 'bob']]
Out[8]:
nancy    300
bob      foo
dtype: object
In [9]:
ser[[4, 3, 1]]
Out[9]:
eric    500
dan     400
bob     foo
dtype: object
In [10]:
ser.iloc[[4, 3, 1]]
Out[10]:
eric    500
dan     400
bob     foo
dtype: object
In [11]:
'bob' in ser #check if there is 'bob' index in serise
Out[11]:
True
In [12]:
ser * 2
Out[12]:
tom         200
bob      foofoo
nancy       600
dan         800
eric       1000
dtype: object
In [13]:
ser
Out[13]:
tom      100
bob      foo
nancy    300
dan      400
eric     500
dtype: object
In [14]:
ser[['nancy','eric',]] ** 2
Out[14]:
nancy     90000
eric     250000
dtype: object
In [15]:
d = { 'one' : pd.Series([100., 200., 300.], index=['apple', 'ball', 'clock']),
      'two' : pd.Series([111., 222., 333., 4444.], index = ['apple','ball','cerill','dancy'])}
In [16]:
d
Out[16]:
{'one': apple    100.0
 ball     200.0
 clock    300.0
 dtype: float64, 'two': apple      111.0
 ball       222.0
 cerill     333.0
 dancy     4444.0
 dtype: float64}
In [17]:
df = pd.DataFrame(d)
df
Out[17]:
onetwo
apple100.0111.0
ball200.0222.0
cerillNaN333.0
clock300.0NaN
dancyNaN4444.0
In [18]:
df.index
Out[18]:
Index(['apple', 'ball', 'cerill', 'clock', 'dancy'], dtype='object')
In [19]:
df.columns
Out[19]:
Index(['one', 'two'], dtype='object')
In [20]:
pd.DataFrame(d, index = [ 'dency' , 'ball' , 'apple' ])
Out[20]:
onetwo
dencyNaNNaN
ball200.0222.0
apple100.0111.0
In [21]:
pd.DataFrame(d, index = ['dency', 'ball' ,'apple'], columns = ['two','five'])
Out[21]:
twofive
dencyNaNNaN
ball222.0NaN
apple111.0NaN
In [22]:
data = [{'alex' : 1, 'joe' : 2}, {'ema' : 5 , 'dora' : 10, 'alice' : 20}]
In [23]:
pd.DataFrame(data)
Out[23]:
alexalicedoraemajoe
01.0NaNNaNNaN2.0
1NaN20.010.05.0NaN
In [24]:
pd.DataFrame(data, index = [ 'orange', 'red' ])
Out[24]:
alexalicedoraemajoe
orange1.0NaNNaNNaN2.0
redNaN20.010.05.0NaN
In [25]:
pd.DataFrame(data, columns = [ 'joe' , 'dora', 'alice'])
Out[25]:
joedoraalice
02.0NaNNaN
1NaN10.020.0
In [26]:
df
Out[26]:
onetwo
apple100.0111.0
ball200.0222.0
cerillNaN333.0
clock300.0NaN
dancyNaN4444.0
In [27]:
df['one']
Out[27]:
apple     100.0
ball      200.0
cerill      NaN
clock     300.0
dancy       NaN
Name: one, dtype: float64
In [28]:
df['three'] = df['one'] * df['two']
df
Out[28]:
onetwothree
apple100.0111.011100.0
ball200.0222.044400.0
cerillNaN333.0NaN
clock300.0NaNNaN
dancyNaN4444.0NaN
In [30]:
df['flag'] = df['one'] > 250
df
Out[30]:
onetwothreeflag
apple100.0111.011100.0False
ball200.0222.044400.0False
cerillNaN333.0NaNFalse
clock300.0NaNNaNTrue
dancyNaN4444.0NaNFalse
In [35]:
three = df.pop('three')
In [34]:
three
Out[34]:
apple     11100.0
ball      44400.0
cerill        NaN
clock         NaN
dancy         NaN
Name: three, dtype: float64
In [37]:
del df['two']
In [39]:
df.insert(2, 'copy_of_one', df['one'])
df
Out[39]:
oneflagcopy_of_one
apple100.0False100.0
ball200.0False200.0
cerillNaNFalseNaN
clock300.0True300.0
dancyNaNFalseNaN
In [40]:
df['one_upper_half'] = df['one'][:2]
df
Out[40]:
oneflagcopy_of_oneone_upper_half
apple100.0False100.0100.0
ball200.0False200.0200.0
cerillNaNFalseNaNNaN
clock300.0True300.0NaN
dancyNaNFalseNaNNaN
In [ ]:


+ Recent posts