[关闭]
@BruceWang 2018-01-03T11:54:05.000000Z 字数 1069 阅读 1117
  1. import pandas as pd
  1. from pandas import Series, DataFrame
  1. obj = pd.Series([4,7,-5,3])
  2. obj
  1. obj.values
  1. obj.index
  1. obj2 = pd.Series([4,5,-5,4], index=['a', 'b','c','ss'])
  2. obj2
  1. obj2['a']
  1. obj2[obj2>0]
  1. obj2*2
  1. import numpy as np
  2. np.exp(obj2)
  1. dict = {'a':3, 'b':4, '()':4}
  1. obj3 = pd.Series(dict)
  2. obj3
  1. states=['a','b',')','()']
  2. obj4 = pd.Series(dict, index=states)
  3. obj4
  1. pd.isnull(obj4)
  1. pd.notnull(obj4)
  1. obj3
  1. obj4
  1. obj3+obj4
  1. data = {'state':['a','v','r','b'],
  2. 'year':[1222,1666,1777,1000],
  3. 'pop':[1,2.5,13,5]}
  4. frame = pd.DataFrame(data)
  5. frame
pop state year 0 1.0 a 1222 1 2.5 v 1666 2 13.0 r 1777 3 5.0 b 1000
  1. frame.head()
pop state year 0 1.0 a 1222 1 2.5 v 1666 2 13.0 r 1777 3 5.0 b 1000
  1. pd.DataFrame(data,columns=['year','state','pop','nothing'])
year state pop nothing 0 1222 a 1.0 NaN 1 1666 v 2.5 NaN 2 1777 r 13.0 NaN 3 1000 b 5.0 NaN
  1. frame.year
  1. frame.pop
  1. frame.values # 返回所有数组
  1. obj = pd.Series(range(3), index=['1','2','a'])
  1. index = obj.index
  2. index
  1. index[2:]
  1. index object 是不可更改的
  1. index[1]='g'
  1. frame
pop state year 0 1.0 a 1222 1 2.5 v 1666 2 13.0 r 1777 3 5.0 b 1000
  1. frame.columns
Index(['pop', 'state', 'year'], dtype='object')

和python不同,pandas允许index有重复labels:

  1. dup = pd.Index(['a','a','c','abr'])
  1. dup
Index(['a', 'a', 'c', 'abr'], dtype='object')
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注