pandas

1

Posted by Deng on April 23, 2019
from pandas import Series,DataFrame
import pandas as pd
import numpy as np

obj = Series([4,7,-5,3])

obj
0    4
1    7
2   -5
3    3
dtype: int64
obj.values
array([ 4,  7, -5,  3])
obj.index
RangeIndex(start=0, stop=4, step=1)
obj2 = Series([4,7,-5,3],index=['d','b','a','c'])
obj2
d    4
b    7
a   -5
c    3
dtype: int64
obj2.index
Index(['d', 'b', 'a', 'c'], dtype='object')
obj2['a']
-5
obj2['d'] = 6
obj2
d    6
b    7
a   -5
c    3
dtype: int64
obj2[['c','a','b']]
c    3
a   -5
b    7
dtype: int64
obj2[obj2>0]
d    6
b    7
c    3
dtype: int64
obj2*2
d    12
b    14
a   -10
c     6
dtype: int64
'b' in obj2
True
'e' in obj2
False
sdata = {'a':1,'b':2,'c':3,'d':4}
obj3 = Series(sdata)
obj3
a    1
b    2
c    3
d    4
dtype: int64
states = ['a','b','c','e']
obj4 = Series(sdata,index=states)
obj4
a    1.0
b    2.0
c    3.0
e    NaN
dtype: float64
pd.isnull(obj4)
a    False
b    False
c    False
e     True
dtype: bool
pd.notnull(obj4)
a     True
b     True
c     True
e    False
dtype: bool
obj3+obj4
a    2.0
b    4.0
c    6.0
d    NaN
e    NaN
dtype: float64
obj4.name= 'population'
obj4.index.name='states'
obj4
states
a    1.0
b    2.0
c    3.0
e    NaN
Name: population, dtype: float64
data = {'states':['o','a','b','c','d'],
       'year':[2000,2001,2002,2003,2004],
       'pop':[1,2,3,4,5]}
frame = DataFrame(data)
frame
states year pop
0 o 2000 1
1 a 2001 2
2 b 2002 3
3 c 2003 4
4 d 2004 5
DataFrame(data,columns=['year','states','pop'])
year states pop
0 2000 o 1
1 2001 a 2
2 2002 b 3
3 2003 c 4
4 2004 d 5
frame2 = DataFrame(data,index=['one','two','three','four','five'],columns=['year','states','pop','debt'])
frame2
year states pop debt
one 2000 o 1 NaN
two 2001 a 2 NaN
three 2002 b 3 NaN
four 2003 c 4 NaN
five 2004 d 5 NaN
frame2.columns
Index(['year', 'states', 'pop', 'debt'], dtype='object')
frame2['states']
one      o
two      a
three    b
four     c
five     d
Name: states, dtype: object
frame2.loc['three']
year      2002
states       b
pop          3
debt       NaN
Name: three, dtype: object
frame2['debt']= 16.5
frame2
year states pop debt
one 2000 o 1 16.5
two 2001 a 2 16.5
three 2002 b 3 16.5
four 2003 c 4 16.5
five 2004 d 5 16.5
frame2['debt'] = np.arange(5)
frame2
year states pop debt
one 2000 o 1 0
two 2001 a 2 1
three 2002 b 3 2
four 2003 c 4 3
five 2004 d 5 4
val = Series([6,7,8],index=['two','three','four'])
frame2['debt'] = val
frame2
year states pop debt
one 2000 o 1 NaN
two 2001 a 2 6.0
three 2002 b 3 7.0
four 2003 c 4 8.0
five 2004 d 5 NaN
frame2['ok']= frame2['pop']==3
frame2
year states pop debt ok
one 2000 o 1 NaN False
two 2001 a 2 6.0 False
three 2002 b 3 7.0 True
four 2003 c 4 8.0 False
five 2004 d 5 NaN False
del frame2['ok']
frame2.columns
Index(['year', 'states', 'pop', 'debt'], dtype='object')