利用python进入数据分析之pandas的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013584315/article/details/78240825

导入相关库

In [2]:
from pandas import Series, DataFrame
import pandas as pd
from __future__ import division
from numpy.random import randn
import numpy as np
import os
import matplotlib.pyplot as plt
np.random.seed(12345)
plt.rc('figure', figsize=(10, 6))
from pandas import Series, DataFrame
import pandas as pd
np.set_printoptions(precision=4)

pandas的数据结构介绍

In [3]:
obj = Series([4, 7, -5, 3]) # 创建数组对象
obj
Out[3]:
0    4
1    7
2   -5
3    3
dtype: int64
In [4]:
obj.values
Out[4]:
array([ 4,  7, -5,  3], dtype=int64)
In [5]:
obj.index
Out[5]:
RangeIndex(start=0, stop=4, step=1)
In [6]:
obj2 = Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
In [7]:
obj2
Out[7]:
d    4
b    7
a   -5
c    3
dtype: int64
In [8]:
obj2.index
Out[8]:
Index([u'd', u'b', u'a', u'c'], dtype='object')
In [9]:
obj2['a']
Out[9]:
-5
In [10]:
obj2['d'] = 6
obj2[['c', 'a', 'd']]
Out[10]:
c    3
a   -5
d    6
dtype: int64
In [11]:
obj2[obj2 > 0]
Out[11]:
d    6
b    7
c    3
dtype: int64
In [12]:
obj2 * 2
Out[12]:
d    12
b    14
a   -10
c     6
dtype: int64
In [13]:
np.exp(obj2)
Out[13]:
d     403.428793
b    1096.633158
a       0.006738
c      20.085537
dtype: float64
In [14]:
'b' in obj2
Out[14]:
True
In [15]:
'e' in obj2
Out[15]:
False
In [16]:
sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
obj3 = Series(sdata)
obj3
Out[16]:
Ohio      35000
Oregon    16000
Texas     71000
Utah       5000
dtype: int64
In [17]:
states = ['California', 'Ohio', 'Oregon', 'Texas']
obj4 = Series(sdata, index=states)
obj4
Out[17]:
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64
In [18]:
pd.isnull(obj4)  #检测是否缺失数据
Out[18]:
California     True
Ohio          False
Oregon        False
Texas         False
dtype: bool
In [19]:
pd.notnull(obj4)
Out[19]:
California    False
Ohio           True
Oregon         True
Texas          True
dtype: bool
In [20]:
obj4.isnull()#检测是否缺失数据
Out[20]:
California     True
Ohio          False
Oregon        False
Texas         False
dtype: bool
In [21]:
obj3
Out[21]:
Ohio      35000
Oregon    16000
Texas     71000
Utah       5000
dtype: int64
In [22]:
obj4
Out[22]:
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64
In [23]:
obj3 + obj4
Out[23]:
California         NaN
Ohio           70000.0
Oregon         32000.0
Texas         142000.0
Utah               NaN
dtype: float64
In [24]:
obj4.name = 'population' # 设置名字
obj4.index.name = 'state'# 设置索引名字
obj4
Out[24]:
state
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
Name: population, dtype: float64
In [25]:
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
obj
Out[25]:
Bob      4
Steve    7
Jeff    -5
Ryan     3
dtype: int64

DataFrame

In [26]:
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': [2000, 2001, 2002, 2001, 2002],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
frame = DataFrame(data)
In [27]:
frame
Out[27]:
  pop state year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002
In [28]:
DataFrame(data, columns=['year', 'state', 'pop']) # 设置列索引
Out[28]:
  year state pop
0 2000 Ohio 1.5
1 2001 Ohio 1.7
2 2002 Ohio 3.6
3 2001 Nevada 2.4
4 2002 Nevada 2.9
In [29]:
frame2 = DataFrame(data, columns=['year', 'state', 'pop', 'debt'],
                   index=['one', 'two', 'three', 'four', 'five'])
frame2
Out[29]:
  year state pop debt
one 2000 Ohio 1.5 NaN
two 2001 Ohio 1.7 NaN
three 2002 Ohio 3.6 NaN
four 2001 Nevada 2.4 NaN
five 2002 Nevada 2.9 NaN
In [30]:
frame2.columns
Out[30]:
Index([u'year', u'state', u'pop', u'debt'], dtype='object')
In [31]:
frame2['state']
Out[31]:
one        Ohio
two        Ohio
three      Ohio
four     Nevada
five     Nevada
Name: state, dtype: object
In [32]:
frame2.year
Out[32]:
one      2000
two      2001
three    2002
four     2001
five     2002
Name: year, dtype: int64
In [33]:
frame2.ix['three'] # 通过ix,索引字段进行索引
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[33]:
year     2002
state    Ohio
pop       3.6
debt      NaN
Name: three, dtype: object
In [34]:
frame2['debt'] = 16.5 # 列赋值
frame2
Out[34]:
  year state pop debt
one 2000 Ohio 1.5 16.5
two 2001 Ohio 1.7 16.5
three 2002 Ohio 3.6 16.5
four 2001 Nevada 2.4 16.5
five 2002 Nevada 2.9 16.5
In [35]:
frame2['debt'] = np.arange(5.)# 列赋值
frame2
Out[35]:
  year state pop debt
one 2000 Ohio 1.5 0.0
two 2001 Ohio 1.7 1.0
three 2002 Ohio 3.6 2.0
four 2001 Nevada 2.4 3.0
five 2002 Nevada 2.9 4.0
In [36]:
val = Series([-1.2, -1.5, -1.7], index=['two', 'four', 'five']) # 指定列赋值
frame2['debt'] = val
frame2
Out[36]:
  year state pop debt
one 2000 Ohio 1.5 NaN
two 2001 Ohio 1.7 -1.2
three 2002 Ohio 3.6 NaN
four 2001 Nevada 2.4 -1.5
five 2002 Nevada 2.9 -1.7
In [37]:
frame2['eastern'] = frame2.state == 'Ohio'
frame2
Out[37]:
  year state pop debt eastern
one 2000 Ohio 1.5 NaN True
two 2001 Ohio 1.7 -1.2 True
three 2002 Ohio 3.6 NaN True
four 2001 Nevada 2.4 -1.5 False
five 2002 Nevada 2.9 -1.7 False
In [38]:
del frame2['eastern']
frame2.columns
Out[38]:
Index([u'year', u'state', u'pop', u'debt'], dtype='object')
In [39]:
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
       'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
In [40]:
frame3 = DataFrame(pop)
frame3
Out[40]:
  Nevada Ohio
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
In [41]:
frame3.T # 转置,行和列互换
Out[41]:
  2000 2001 2002
Nevada NaN 2.4 2.9
Ohio 1.5 1.7 3.6
In [42]:
DataFrame(pop, index=[2001, 2002, 2003])
Out[42]:
  Nevada Ohio
2001 2.4 1.7
2002 2.9 3.6
2003 NaN NaN
In [43]:
pdata = {'Ohio': frame3['Ohio'][:-1],
         'Nevada': frame3['Nevada'][:2]}
DataFrame(pdata)
Out[43]:
  Nevada Ohio
2000 NaN 1.5
2001 2.4 1.7
In [44]:
frame3.index.name = 'year'; frame3.columns.name = 'state'
frame3
Out[44]:
state Nevada Ohio
year    
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
In [45]:
frame3.values # DF返回二维数组
Out[45]:
array([[ nan,  1.5],
       [ 2.4,  1.7],
       [ 2.9,  3.6]])
In [46]:
frame2.values
Out[46]:
array([[2000L, 'Ohio', 1.5, nan],
       [2001L, 'Ohio', 1.7, -1.2],
       [2002L, 'Ohio', 3.6, nan],
       [2001L, 'Nevada', 2.4, -1.5],
       [2002L, 'Nevada', 2.9, -1.7]], dtype=object)

索引对象

In [47]:
obj = Series(range(3), index=['a', 'b', 'c'])
index = obj.index
index
Out[47]:
Index([u'a', u'b', u'c'], dtype='object')
In [48]:
index[1:]
Out[48]:
Index([u'b', u'c'], dtype='object')
In [49]:
index[1] = 'd' #索引对象不支持更改
TypeErrorTraceback (most recent call last)
<ipython-input-49-676fdeb26a68> in <module>()
----> 1 index[1] = 'd'

D:\python2713\lib\anaconda_install\lib\site-packages\pandas\core\indexes\base.pyc in __setitem__(self, key, value)
   1618 
   1619     def __setitem__(self, key, value):
-> 1620         raise TypeError("Index does not support mutable operations")
   1621 
   1622     def __getitem__(self, key):

TypeError: Index does not support mutable operations
In [51]:
index = pd.Index(np.arange(3))
index
Out[51]:
Int64Index([0, 1, 2], dtype='int64')
In [52]:
obj2 = Series([1.5, -2.5, 0], index=index)
obj2
Out[52]:
0    1.5
1   -2.5
2    0.0
dtype: float64
In [53]:
obj2.index is index
Out[53]:
True
In [54]:
frame3
Out[54]:
state Nevada Ohio
year    
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
In [55]:
'Ohio' in frame3.columns
Out[55]:
True
In [56]:
2003 in frame3.index
Out[56]:
False

基本功能

重建索引

In [58]:
obj = Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])
obj
Out[58]:
d    4.5
b    7.2
a   -5.3
c    3.6
dtype: float64
In [59]:
obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])
obj2
Out[59]:
a   -5.3
b    7.2
c    3.6
d    4.5
e    NaN
dtype: float64
In [60]:
obj.reindex(['a', 'b', 'c', 'd', 'e'], fill_value=0) # 重新根据索引排序,有缺失值填入fill_value
Out[60]:
a   -5.3
b    7.2
c    3.6
d    4.5
e    0.0
dtype: float64
In [61]:
obj3 = Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3.reindex(range(6), method='ffill') # 向前填充
Out[61]:
0      blue
1      blue
2    purple
3    purple
4    yellow
5    yellow
dtype: object
In [62]:
frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'],
                  columns=['Ohio', 'Texas', 'California'])
frame
Out[62]:
  Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8
In [63]:
frame2 = frame.reindex(['a', 'b', 'c', 'd'])
frame2
Out[63]:
  Ohio Texas California
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0
In [64]:
states = ['Texas', 'Utah', 'California']
frame.reindex(columns=states)
Out[64]:
  Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8
In [66]:
frame.ix[['a', 'b', 'c', 'd'], states]
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[66]:
  Texas Utah California
a 1.0 NaN 2.0
b NaN NaN NaN
c 4.0 NaN 5.0
d 7.0 NaN 8.0

丢弃指定轴上的项

In [67]:
obj = Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
new_obj = obj.drop('c')
new_obj
Out[67]:
a    0.0
b    1.0
d    3.0
e    4.0
dtype: float64
In [68]:
obj.drop(['d', 'c'])
Out[68]:
a    0.0
b    1.0
e    4.0
dtype: float64
In [70]:
data = DataFrame(np.arange(16).reshape((4, 4)),
                 index=['Ohio', 'Colorado', 'Utah', 'New York'],
                 columns=['one', 'two', 'three', 'four'])
In [71]:
data
Out[71]:
  one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [72]:
data.drop(['Colorado', 'Ohio'])
Out[72]:
  one two three four
Utah 8 9 10 11
New York 12 13 14 15
In [73]:
data.drop('two', axis=1)
Out[73]:
  one three four
Ohio 0 2 3
Colorado 4 6 7
Utah 8 10 11
New York 12 14 15
In [74]:
data.drop(['two', 'four'], axis=1)
Out[74]:
  one three
Ohio 0 2
Colorado 4 6
Utah 8 10
New York 12 14

索引、选取、过滤

In [77]:
obj = Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
obj
Out[77]:
a    0.0
b    1.0
c    2.0
d    3.0
dtype: float64
In [78]:
obj['b']
Out[78]:
1.0
In [79]:
obj[1]
Out[79]:
1.0
In [80]:
obj[2:4]
Out[80]:
c    2.0
d    3.0
dtype: float64
In [81]:
obj[['b', 'a', 'd']]
Out[81]:
b    1.0
a    0.0
d    3.0
dtype: float64
In [82]:
obj[[1, 3]]
Out[82]:
b    1.0
d    3.0
dtype: float64
In [83]:
obj[obj < 2]
Out[83]:
a    0.0
b    1.0
dtype: float64
In [84]:
obj['b':'c']
Out[84]:
b    1.0
c    2.0
dtype: float64
In [85]:
obj['b':'c'] = 5
obj
Out[85]:
a    0.0
b    5.0
c    5.0
d    3.0
dtype: float64
In [86]:
data = DataFrame(np.arange(16).reshape((4, 4)),
                 index=['Ohio', 'Colorado', 'Utah', 'New York'],
                 columns=['one', 'two', 'three', 'four'])
data
Out[86]:
  one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [87]:
data['two']
Out[87]:
Ohio         1
Colorado     5
Utah         9
New York    13
Name: two, dtype: int32
In [88]:
data[['three', 'one']]
Out[88]:
  three one
Ohio 2 0
Colorado 6 4
Utah 10 8
New York 14 12
In [89]:
data[:2]
Out[89]:
  one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
In [90]:
data[data['three'] > 5]
Out[90]:
  one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [91]:
data < 5
Out[91]:
  one two three four
Ohio True True True True
Colorado True False False False
Utah False False False False
New York False False False False
In [92]:
data[data < 5] = 0
In [93]:
data
Out[93]:
  one two three four
Ohio 0 0 0 0
Colorado 0 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
In [94]:
data.ix['Colorado', ['two', 'three']]
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[94]:
two      5
three    6
Name: Colorado, dtype: int32
In [95]:
data.ix[['Colorado', 'Utah'], [3, 0, 1]]
Out[95]:
  four one two
Colorado 7 0 5
Utah 11 8 9
In [96]:
data.ix[2] # 选取单个列
Out[96]:
one       8
two       9
three    10
four     11
Name: Utah, dtype: int32
In [97]:
data.ix[:'Utah', 'two']
Out[97]:
Ohio        0
Colorado    5
Utah        9
Name: two, dtype: int32
In [98]:
data.ix[:'Utah', 'two']
Out[98]:
Ohio        0
Colorado    5
Utah        9
Name: two, dtype: int32

算数运算和数据对齐

In [99]:
s1 = Series([7.3, -2.5, 3.4, 1.5], index=['a', 'c', 'd', 'e'])
s2 = Series([-2.1, 3.6, -1.5, 4, 3.1], index=['a', 'c', 'e', 'f', 'g'])
In [100]:
s1
Out[100]:
a    7.3
c   -2.5
d    3.4
e    1.5
dtype: float64
In [101]:
s2
Out[101]:
a   -2.1
c    3.6
e   -1.5
f    4.0
g    3.1
dtype: float64
In [102]:
s1 + s2
Out[102]:
a    5.2
c    1.1
d    NaN
e    0.0
f    NaN
g    NaN
dtype: float64
In [103]:
df1 = DataFrame(np.arange(9.).reshape((3, 3)), columns=list('bcd'),
                index=['Ohio', 'Texas', 'Colorado'])
df2 = DataFrame(np.arange(12.).reshape((4, 3)), columns=list('bde'),
                index=['Utah', 'Ohio', 'Texas', 'Oregon'])
df1
Out[103]:
  b c d
Ohio 0.0 1.0 2.0
Texas 3.0 4.0 5.0
Colorado 6.0 7.0 8.0
In [104]:
df2
Out[104]:
  b d e
Utah 0.0 1.0 2.0
Ohio 3.0 4.0 5.0
Texas 6.0 7.0 8.0
Oregon 9.0 10.0 11.0
In [105]:
df1 + df2 # 空值用NaN代替
Out[105]:
  b c d e
Colorado NaN NaN NaN NaN
Ohio 3.0 NaN 6.0 NaN
Oregon NaN NaN NaN NaN
Texas 9.0 NaN 12.0 NaN
Utah NaN NaN NaN NaN

在算数方法中填充值

In [106]:
df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns=list('abcd'))
df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns=list('abcde'))
df1
Out[106]:
  a b c d
0 0.0 1.0 2.0 3.0
1 4.0 5.0 6.0 7.0
2 8.0 9.0 10.0 11.0
In [107]:
df2
Out[107]:
  a b c d e
0 0.0 1.0 2.0 3.0 4.0
1 5.0 6.0 7.0 8.0 9.0
2 10.0 11.0 12.0 13.0 14.0
3 15.0 16.0 17.0 18.0 19.0
In [108]:
df1 + df2
Out[108]:
  a b c d e
0 0.0 2.0 4.0 6.0 NaN
1 9.0 11.0 13.0 15.0 NaN
2 18.0 20.0 22.0 24.0 NaN
3 NaN NaN NaN NaN NaN
In [109]:
df1.add(df2, fill_value=0)
Out[109]:
  a b c d e
0 0.0 2.0 4.0 6.0 4.0
1 9.0 11.0 13.0 15.0 9.0
2 18.0 20.0 22.0 24.0 14.0
3 15.0 16.0 17.0 18.0 19.0
In [110]:
df1.reindex(columns=df2.columns, fill_value=0)
Out[110]:
  a b c d e
0 0.0 1.0 2.0 3.0 0
1 4.0 5.0 6.0 7.0 0
2 8.0 9.0 10.0 11.0 0

DataFrame和Series间的运算

In [111]:
arr = np.arange(12.).reshape((3, 4))
arr
Out[111]:
array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]])
In [112]:
arr[0]
Out[112]:
array([ 0.,  1.,  2.,  3.])
In [113]:
arr - arr[0]
Out[113]:
array([[ 0.,  0.,  0.,  0.],
       [ 4.,  4.,  4.,  4.],
       [ 8.,  8.,  8.,  8.]])
In [114]:
frame = DataFrame(np.arange(12.).reshape((4, 3)), columns=list('bde'),
                  index=['Utah', 'Ohio', 'Texas', 'Oregon'])
series = frame.ix[0]
frame
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  This is separate from the ipykernel package so we can avoid doing imports until
Out[114]:
  b d e
Utah 0.0 1.0 2.0
Ohio 3.0 4.0 5.0
Texas 6.0 7.0 8.0
Oregon 9.0 10.0 11.0
In [115]:
series
Out[115]:
b    0.0
d    1.0
e    2.0
Name: Utah, dtype: float64
In [116]:
frame - series
Out[116]:
  b d e
Utah 0.0 0.0 0.0
Ohio 3.0 3.0 3.0
Texas 6.0 6.0 6.0
Oregon 9.0 9.0 9.0
In [117]:
series2 = Series(range(3), index=['b', 'e', 'f'])
frame + series2
Out[117]:
  b d e f
Utah 0.0 NaN 3.0 NaN
Ohio 3.0 NaN 6.0 NaN
Texas 6.0 NaN 9.0 NaN
Oregon 9.0 NaN 12.0 NaN
In [118]:
series3 = frame['d']
frame
Out[118]:
  b d e
Utah 0.0 1.0 2.0
Ohio 3.0 4.0 5.0
Texas 6.0 7.0 8.0
Oregon 9.0 10.0 11.0
In [119]:
series3
Out[119]:
Utah       1.0
Ohio       4.0
Texas      7.0
Oregon    10.0
Name: d, dtype: float64
In [120]:
frame.sub(series3, axis=0)
Out[120]:
  b d e
Utah -1.0 0.0 1.0
Ohio -1.0 0.0 1.0
Texas -1.0 0.0 1.0
Oregon -1.0 0.0 1.0

函数应用和映射

In [121]:
frame = DataFrame(np.random.randn(4, 3), columns=list('bde'),
                  index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [122]:
frame
Out[122]:
  b d e
Utah -0.204708 0.478943 -0.519439
Ohio -0.555730 1.965781 1.393406
Texas 0.092908 0.281746 0.769023
Oregon 1.246435 1.007189 -1.296221
In [123]:
np.abs(frame) #求绝对值
Out[123]:
  b d e
Utah 0.204708 0.478943 0.519439
Ohio 0.555730 1.965781 1.393406
Texas 0.092908 0.281746 0.769023
Oregon 1.246435 1.007189 1.296221
In [124]:
f = lambda x: x.max() - x.min()
In [125]:
frame.apply(f)
Out[125]:
b    1.802165
d    1.684034
e    2.689627
dtype: float64
In [126]:
frame.apply(f, axis=1)
Out[126]:
Utah      0.998382
Ohio      2.521511
Texas     0.676115
Oregon    2.542656
dtype: float64
In [127]:
def f(x):
    return Series([x.min(), x.max()], index=['min', 'max'])
frame.apply(f)
Out[127]:
  b d e
min -0.555730 0.281746 -1.296221
max 1.246435 1.965781 1.393406
In [128]:
format = lambda x: '%.2f' % x
frame.applymap(format)
Out[128]:
  b d e
Utah -0.20 0.48 -0.52
Ohio -0.56 1.97 1.39
Texas 0.09 0.28 0.77
Oregon 1.25 1.01 -1.30
In [129]:
frame['e'].map(format)
Out[129]:
Utah      -0.52
Ohio       1.39
Texas      0.77
Oregon    -1.30
Name: e, dtype: object

排序和排名

In [130]:
obj = Series(range(4), index=['d', 'a', 'b', 'c'])
obj
Out[130]:
d    0
a    1
b    2
c    3
dtype: int64
In [131]:
obj.sort_index()
Out[131]:
a    1
b    2
c    3
d    0
dtype: int64
In [132]:
frame = DataFrame(np.arange(8).reshape((2, 4)), index=['three', 'one'],
                  columns=['d', 'a', 'b', 'c'])
frame.sort_index()
Out[132]:
  d a b c
one 4 5 6 7
three 0 1 2 3
In [133]:
frame.sort_index(axis=1)
Out[133]:
  a b c d
three 1 2 3 0
one 5 6 7 4
In [134]:
frame.sort_index(axis=1, ascending=False)
Out[134]:
  d c b a
three 0 3 2 1
one 4 7 6 5
In [137]:
frame = DataFrame({'b': [4, 7, -3, 2], 'a': [0, 1, 0, 1]})
frame
Out[137]:
  a b
0 0 4
1 1 7
2 0 -3
3 1 2
In [138]:
frame.sort_index(by='b') #将b列按从小到大排序
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)
  """Entry point for launching an IPython kernel.
Out[138]:
  a b
2 0 -3
3 1 2
0 0 4
1 1 7
In [139]:
frame.sort_index(by=['a', 'b']) # a,b列从小到大排列
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)
  """Entry point for launching an IPython kernel.
Out[139]:
  a b
2 0 -3
0 0 4
3 1 2
1 1 7
In [140]:
obj = Series([7, -5, 7, 4, 2, 0, 4])
obj.rank() # 排名
Out[140]:
0    6.5
1    1.0
2    6.5
3    4.5
4    3.0
5    2.0
6    4.5
dtype: float64
In [141]:
obj.rank(method='first')# 出现的顺序进行排名
Out[141]:
0    6.0
1    1.0
2    7.0
3    4.0
4    3.0
5    2.0
6    5.0
dtype: float64
In [142]:
obj.rank(ascending=False, method='max') #姜旭排名
Out[142]:
0    2.0
1    7.0
2    2.0
3    4.0
4    5.0
5    6.0
6    4.0
dtype: float64
In [143]:
frame = DataFrame({'b': [4.3, 7, -3, 2], 'a': [0, 1, 0, 1],
                   'c': [-2, 5, 8, -2.5]})
frame
Out[143]:
  a b c
0 0 4.3 -2.0
1 1 7.0 5.0
2 0 -3.0 8.0
3 1 2.0 -2.5
In [144]:
frame.rank(axis=1)
Out[144]:
  a b c
0 2.0 3.0 1.0
1 1.0 3.0 2.0
2 2.0 1.0 3.0
3 2.0 3.0 1.0

带有重复值得轴索引

In [145]:
obj = Series(range(5), index=['a', 'a', 'b', 'b', 'c'])
obj
Out[145]:
a    0
a    1
b    2
b    3
c    4
dtype: int64
In [146]:
obj.index.is_unique
Out[146]:
False
In [147]:
obj['a']
Out[147]:
a    0
a    1
dtype: int64
In [148]:
obj['c']
Out[148]:
4
In [149]:
df = DataFrame(np.random.randn(4, 3), index=['a', 'a', 'b', 'b'])
df
Out[149]:
  0 1 2
a 0.274992 0.228913 1.352917
a 0.886429 -2.001637 -0.371843
b 1.669025 -0.438570 -0.539741
b 0.476985 3.248944 -1.021228
In [150]:
df.ix['b']
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[150]:
  0 1 2
b 1.669025 -0.438570 -0.539741
b 0.476985 3.248944 -1.021228

汇总和计算描述统计

In [151]:
df = DataFrame([[1.4, np.nan], [7.1, -4.5],
                [np.nan, np.nan], [0.75, -1.3]],
               index=['a', 'b', 'c', 'd'],
               columns=['one', 'two'])
df
Out[151]:
  one two
a 1.40 NaN
b 7.10 -4.5
c NaN NaN
d 0.75 -1.3
In [152]:
df.sum() # 求和(按列)
Out[152]:
one    9.25
two   -5.80
dtype: float64
In [153]:
df.sum(axis=1)# 求和(按行)
Out[153]:
a    1.40
b    2.60
c    0.00
d   -0.55
dtype: float64
In [154]:
df.mean(axis=1, skipna=False)# 求平均值(按行)
Out[154]:
a      NaN
b    1.300
c      NaN
d   -0.275
dtype: float64
In [155]:
df.idxmax() # 最大的值的标签
Out[155]:
one    b
two    d
dtype: object
In [156]:
df.cumsum() # 累加和
Out[156]:
  one two
a 1.40 NaN
b 8.50 -4.5
c NaN NaN
d 9.25 -5.8
In [157]:
df.describe() #汇总多个统计数据
Out[157]:
  one two
count 3.000000 2.000000
mean 3.083333 -2.900000
std 3.493685 2.262742
min 0.750000 -4.500000
25% 1.075000 -3.700000
50% 1.400000 -2.900000
75% 4.250000 -2.100000
max 7.100000 -1.300000
In [158]:
obj = Series(['a', 'a', 'b', 'c'] * 4)
obj.describe()
Out[158]:
count     16
unique     3
top        a
freq       8
dtype: object
In [ ]:
### 唯一值,估计值以及成员资格
In [160]:
obj = Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])
In [161]:
uniques = obj.unique()
uniques
Out[161]:
array(['c', 'a', 'd', 'b'], dtype=object)
In [162]:
obj.value_counts()
Out[162]:
c    3
a    3
b    2
d    1
dtype: int64
In [163]:
pd.value_counts(obj.values, sort=False) #降频排列
Out[163]:
a    3
c    3
b    2
d    1
dtype: int64
In [164]:
mask = obj.isin(['b', 'c']) #判断是否包含
mask
Out[164]:
0     True
1    False
2    False
3    False
4    False
5     True
6     True
7     True
8     True
dtype: bool
In [165]:
obj[mask]
Out[165]:
0    c
5    b
6    b
7    c
8    c
dtype: object
In [166]:
data = DataFrame({'Qu1': [1, 3, 4, 3, 4],
                  'Qu2': [2, 3, 1, 2, 3],
                  'Qu3': [1, 5, 2, 4, 4]})
data
Out[166]:
  Qu1 Qu2 Qu3
0 1 2 1
1 3 3 5
2 4 1 2
3 3 2 4
4 4 3 4
In [167]:
result = data.apply(pd.value_counts).fillna(0)
result
Out[167]:
  Qu1 Qu2 Qu3
1 1.0 1.0 1.0
2 0.0 2.0 1.0
3 2.0 2.0 0.0
4 2.0 0.0 2.0
5 0.0 0.0 1.0

处理缺失数据

In [168]:
string_data = Series(['aardvark', 'artichoke', np.nan, 'avocado'])
string_data
Out[168]:
0     aardvark
1    artichoke
2          NaN
3      avocado
dtype: object
In [169]:
string_data.isnull()
Out[169]:
0    False
1    False
2     True
3    False
dtype: bool
In [170]:
string_data[0] = None
string_data.isnull()
Out[170]:
0     True
1    False
2     True
3    False
dtype: bool

过滤缺失的数据

In [171]:
from numpy import nan as NA
data = Series([1, NA, 3.5, NA, 7])
data.dropna() #干掉缺失的数据
Out[171]:
0    1.0
2    3.5
4    7.0
dtype: float64
In [172]:
data[data.notnull()]
Out[172]:
0    1.0
2    3.5
4    7.0
dtype: float64
In [173]:
data = DataFrame([[1., 6.5, 3.], [1., NA, NA],
                  [NA, NA, NA], [NA, 6.5, 3.]])
cleaned = data.dropna() # 一行中只要有all就会被干掉
data
Out[173]:
  0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 NaN 6.5 3.0
In [174]:
cleaned
Out[174]:
  0 1 2
0 1.0 6.5 3.0
In [175]:
data.dropna(how='all') # 只干掉全为na的行
Out[175]:
  0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
3 NaN 6.5 3.0
In [176]:
data[4] = NA
data
Out[176]:
  0 1 2 4
0 1.0 6.5 3.0 NaN
1 1.0 NaN NaN NaN
2 NaN NaN NaN NaN
3 NaN 6.5 3.0 NaN
In [178]:
data.dropna(axis=1, how='all') # axis = 1,干掉全为NA的列
Out[178]:
  0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 NaN 6.5 3.0
In [179]:
df = DataFrame(np.random.randn(7, 3))
df.ix[:4, 1] = NA; df.ix[:2, 2] = NA
df
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  
Out[179]:
  0 1 2
0 -0.577087 NaN NaN
1 0.523772 NaN NaN
2 -0.713544 NaN NaN
3 -1.860761 NaN 0.560145
4 -1.265934 NaN -1.063512
5 0.332883 -2.359419 -0.199543
6 -1.541996 -0.970736 -1.307030
In [180]:
df.dropna(thresh=3) #留一部分观测数据
Out[180]:
  0 1 2
5 0.332883 -2.359419 -0.199543
6 -1.541996 -0.970736 -1.307030

填充缺失数据

In [181]:
df.fillna(0) #用0来填充缺失的数据
Out[181]:
  0 1 2
0 -0.577087 0.000000 0.000000
1 0.523772 0.000000 0.000000
2 -0.713544 0.000000 0.000000
3 -1.860761 0.000000 0.560145
4 -1.265934 0.000000 -1.063512
5 0.332883 -2.359419 -0.199543
6 -1.541996 -0.970736 -1.307030
In [182]:
df.fillna({1: 0.5, 3: -1})
Out[182]:
  0 1 2
0 -0.577087 0.500000 NaN
1 0.523772 0.500000 NaN
2 -0.713544 0.500000 NaN
3 -1.860761 0.500000 0.560145
4 -1.265934 0.500000 -1.063512
5 0.332883 -2.359419 -0.199543
6 -1.541996 -0.970736 -1.307030
In [185]:
# 通常会返回新对象,但也可以对现有对象进行就地修改
_ = df.fillna(0, inplace=True)
df
Out[185]:
  0 1 2
0 0.286350 0.377984 -0.753887
1 0.331286 1.349742 0.069877
2 0.246674 0.000000 1.004812
3 1.327195 0.000000 -1.549106
4 0.022185 0.000000 0.000000
5 0.862580 0.000000 0.000000
In [187]:
df = DataFrame(np.random.randn(6, 3))
df.ix[2:, 1] = NA; df.ix[4:, 2] = NA
df
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  
Out[187]:
  0 1 2
0 0.670216 0.852965 -0.955869
1 -0.023493 -2.304234 -0.652469
2 -1.218302 NaN 1.074623
3 0.723642 NaN 1.001543
4 -0.503087 NaN NaN
5 -0.726213 NaN NaN
In [186]:
df.fillna(method='ffill')
Out[186]:
  0 1 2
0 0.286350 0.377984 -0.753887
1 0.331286 1.349742 0.069877
2 0.246674 0.000000 1.004812
3 1.327195 0.000000 -1.549106
4 0.022185 0.000000 0.000000
5 0.862580 0.000000 0.000000
In [188]:
df.fillna(method='ffill', limit=2)
Out[188]:
  0 1 2
0 0.670216 0.852965 -0.955869
1 -0.023493 -2.304234 -0.652469
2 -1.218302 -2.304234 1.074623
3 0.723642 -2.304234 1.001543
4 -0.503087 NaN 1.001543
5 -0.726213 NaN 1.001543
In [189]:
data = Series([1., NA, 3.5, NA, 7])
data.fillna(data.mean())
Out[189]:
0    1.000000
1    3.833333
2    3.500000
3    3.833333
4    7.000000
dtype: float64

层次化索引

In [190]:
data = Series(np.random.randn(10),
              index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],
                     [1, 2, 3, 1, 2, 3, 1, 2, 2, 3]])
data
Out[190]:
a  1   -1.157719
   2    0.816707
   3    0.433610
b  1    1.010737
   2    1.824875
   3   -0.997518
c  1    0.850591
   2   -0.131578
d  2    0.912414
   3    0.188211
dtype: float64
In [191]:
data.index
Out[191]:
MultiIndex(levels=[[u'a', u'b', u'c', u'd'], [1, 2, 3]],
           labels=[[0, 0, 0, 1, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 1, 2]])
In [192]:
data['b']
Out[192]:
1    1.010737
2    1.824875
3   -0.997518
dtype: float64
In [193]:
data['b':'c']
Out[193]:
b  1    1.010737
   2    1.824875
   3   -0.997518
c  1    0.850591
   2   -0.131578
dtype: float64
In [194]:
data.ix[['b', 'd']]
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[194]:
b  1    1.010737
   2    1.824875
   3   -0.997518
d  2    0.912414
   3    0.188211
dtype: float64
In [195]:
data[:, 2]
Out[195]:
a    0.816707
b    1.824875
c   -0.131578
d    0.912414
dtype: float64
In [196]:
data.unstack()
Out[196]:
  1 2 3
a -1.157719 0.816707 0.433610
b 1.010737 1.824875 -0.997518
c 0.850591 -0.131578 NaN
d NaN 0.912414 0.188211
In [197]:
data.unstack().stack()
Out[197]:
a  1   -1.157719
   2    0.816707
   3    0.433610
b  1    1.010737
   2    1.824875
   3   -0.997518
c  1    0.850591
   2   -0.131578
d  2    0.912414
   3    0.188211
dtype: float64
In [198]:
frame = DataFrame(np.arange(12).reshape((4, 3)),
                  index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],
                  columns=[['Ohio', 'Ohio', 'Colorado'],
                           ['Green', 'Red', 'Green']])
frame
Out[198]:
    Ohio Colorado
    Green Red Green
a 1 0 1 2
2 3 4 5
b 1 6 7 8
2 9 10 11
In [199]:
frame.index.names = ['key1', 'key2']
frame.columns.names = ['state', 'color']
frame
Out[199]:
  state Ohio Colorado
  color Green Red Green
key1 key2      
a 1 0 1 2
2 3 4 5
b 1 6 7 8
2 9 10 11
In [200]:
frame['Ohio']
Out[200]:
  color Green Red
key1 key2    
a 1 0 1
2 3 4
b 1 6 7
2 9 10

重排分级顺序

In [202]:
frame.swaplevel('key1', 'key2')
Out[202]:
  state Ohio Colorado
  color Green Red Green
key2 key1      
1 a 0 1 2
2 a 3 4 5
1 b 6 7 8
2 b 9 10 11
In [203]:
frame.sortlevel(1)
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: sortlevel is deprecated, use sort_index(level= ...)
  """Entry point for launching an IPython kernel.
Out[203]:
  state Ohio Colorado
  color Green Red Green
key1 key2      
a 1 0 1 2
b 1 6 7 8
a 2 3 4 5
b 2 9 10 11
In [204]:
frame.swaplevel(0, 1).sortlevel(0)
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: sortlevel is deprecated, use sort_index(level= ...)
  """Entry point for launching an IPython kernel.
Out[204]:
  state Ohio Colorado
  color Green Red Green
key2 key1      
1 a 0 1 2
b 6 7 8
2 a 3 4 5
b 9 10 11

根据级别汇总统计

In [205]:
frame.sum(level='key2')
Out[205]:
state Ohio Colorado
color Green Red Green
key2      
1 6 8 10
2 12 14 16
In [206]:
frame.sum(level='color', axis=1)
Out[206]:
  color Green Red
key1 key2    
a 1 2 1
2 8 4
b 1 14 7
2 20 10

使用DataFrame的列

In [207]:
frame = DataFrame({'a': range(7), 'b': range(7, 0, -1),
                   'c': ['one', 'one', 'one', 'two', 'two', 'two', 'two'],
                   'd': [0, 1, 2, 0, 1, 2, 3]})
frame
Out[207]:
  a b c d
0 0 7 one 0
1 1 6 one 1
2 2 5 one 2
3 3 4 two 0
4 4 3 two 1
5 5 2 two 2
6 6 1 two 3
In [208]:
frame2 = frame.set_index(['c', 'd'])
frame2
Out[208]:
    a b
c d    
one 0 0 7
1 1 6
2 2 5
two 0 3 4
1 4 3
2 5 2
3 6 1
In [209]:
frame.set_index(['c', 'd'], drop=False)
Out[209]:
    a b c d
c d        
one 0 0 7 one 0
1 1 6 one 1
2 2 5 one 2
two 0 3 4 two 0
1 4 3 two 1
2 5 2 two 2
3 6 1 two 3
In [210]:
frame2.reset_index()
Out[210]:
  c d a b
0 one 0 0 7
1 one 1 1 6
2 one 2 2 5
3 two 0 3 4
4 two 1 4 3
5 two 2 5 2
6 two 3 6 1

其他关于 pandas话题

整数索引

In [211]:
ser = Series(np.arange(3.))
ser.iloc[-1]
Out[211]:
2.0
In [212]:
ser
Out[212]:
0    0.0
1    1.0
2    2.0
dtype: float64
In [213]:
ser2 = Series(np.arange(3.), index=['a', 'b', 'c'])
ser2[-1]
Out[213]:
2.0
In [214]:
ser.ix[:1]
D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
Out[214]:
0    0.0
1    1.0
dtype: float64
In [215]:
ser3 = Series(range(3), index=[-5, 1, 3])
ser3
Out[215]:
-5    0
 1    1
 3    2
dtype: int64
In [216]:
ser3.iloc[2]
Out[216]:
2
In [219]:
frame = DataFrame(np.arange(6).reshape((3, 2)), index=[2, 0, 1])
frame.iloc[0]
Out[219]:
0    0
1    1
Name: 2, dtype: int32
展开阅读全文

没有更多推荐了,返回首页