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

导入相关库

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]:
popstateyear
01.5Ohio2000
11.7Ohio2001
23.6Ohio2002
32.4Nevada2001
42.9Nevada2002
In [28]:
DataFrame(data, columns=['year', 'state', 'pop']) # 设置列索引
Out[28]:
yearstatepop
02000Ohio1.5
12001Ohio1.7
22002Ohio3.6
32001Nevada2.4
42002Nevada2.9
In [29]:
frame2 = DataFrame(data, columns=['year', 'state', 'pop', 'debt'],
                   index=['one', 'two', 'three', 'four', 'five'])
frame2
Out[29]:
yearstatepopdebt
one2000Ohio1.5NaN
two2001Ohio1.7NaN
three2002Ohio3.6NaN
four2001Nevada2.4NaN
five2002Nevada2.9NaN
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]:
yearstatepopdebt
one2000Ohio1.516.5
two2001Ohio1.716.5
three2002Ohio3.616.5
four2001Nevada2.416.5
five2002Nevada2.916.5
In [35]:
frame2['debt'] = np.arange(5.)# 列赋值
frame2
Out[35]:
yearstatepopdebt
one2000Ohio1.50.0
two2001Ohio1.71.0
three2002Ohio3.62.0
four2001Nevada2.43.0
five2002Nevada2.94.0
In [36]:
val = Series([-1.2, -1.5, -1.7], index=['two', 'four', 'five']) # 指定列赋值
frame2['debt'] = val
frame2
Out[36]:
yearstatepopdebt
one2000Ohio1.5NaN
two2001Ohio1.7-1.2
three2002Ohio3.6NaN
four2001Nevada2.4-1.5
five2002Nevada2.9-1.7
In [37]:
frame2['eastern'] = frame2.state == 'Ohio'
frame2
Out[37]:
yearstatepopdebteastern
one2000Ohio1.5NaNTrue
two2001Ohio1.7-1.2True
three2002Ohio3.6NaNTrue
four2001Nevada2.4-1.5False
five2002Nevada2.9-1.7False
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]:
NevadaOhio
2000NaN1.5
20012.41.7
20022.93.6
In [41]:
frame3.T # 转置,行和列互换
Out[41]:
200020012002
NevadaNaN2.42.9
Ohio1.51.73.6
In [42]:
DataFrame(pop, index=[2001, 2002, 2003])
Out[42]:
NevadaOhio
20012.41.7
20022.93.6
2003NaNNaN
In [43]:
pdata = {'Ohio': frame3['Ohio'][:-1],
         'Nevada': frame3['Nevada'][:2]}
DataFrame(pdata)
Out[43]:
NevadaOhio
2000NaN1.5
20012.41.7
In [44]:
frame3.index.name = 'year'; frame3.columns.name = 'state'
frame3
Out[44]:
stateNevadaOhio
year
2000NaN1.5
20012.41.7
20022.93.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]:
stateNevadaOhio
year
2000NaN1.5
20012.41.7
20022.93.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]:
OhioTexasCalifornia
a012
c345
d678
In [63]:
frame2 = frame.reindex(['a', 'b', 'c', 'd'])
frame2
Out[63]:
OhioTexasCalifornia
a0.01.02.0
bNaNNaNNaN
c3.04.05.0
d6.07.08.0
In [64]:
states = ['Texas', 'Utah', 'California']
frame.reindex(columns=states)
Out[64]:
TexasUtahCalifornia
a1NaN2
c4NaN5
d7NaN8
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]:
TexasUtahCalifornia
a1.0NaN2.0
bNaNNaNNaN
c4.0NaN5.0
d7.0NaN8.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]:
onetwothreefour
Ohio0123
Colorado4567
Utah891011
New York12131415
In [72]:
data.drop(['Colorado', 'Ohio'])
Out[72]:
onetwothreefour
Utah891011
New York12131415
In [73]:
data.drop('two', axis=1)
Out[73]:
onethreefour
Ohio023
Colorado467
Utah81011
New York121415
In [74]:
data.drop(['two', 'four'], axis=1)
Out[74]:
onethree
Ohio02
Colorado46
Utah810
New York1214

索引、选取、过滤

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]:
onetwothreefour
Ohio0123
Colorado4567
Utah891011
New York12131415
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]:
threeone
Ohio20
Colorado64
Utah108
New York1412
In [89]:
data[:2]
Out[89]:
onetwothreefour
Ohio0123
Colorado4567
In [90]:
data[data['three'] > 5]
Out[90]:
onetwothreefour
Colorado4567
Utah891011
New York12131415
In [91]:
data < 5
Out[91]:
onetwothreefour
OhioTrueTrueTrueTrue
ColoradoTrueFalseFalseFalse
UtahFalseFalseFalseFalse
New YorkFalseFalseFalseFalse
In [92]:
data[data < 5] = 0
In [93]:
data
Out[93]:
onetwothreefour
Ohio0000
Colorado0567
Utah891011
New York12131415
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]:
fouronetwo
Colorado705
Utah1189
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]:
bcd
Ohio0.01.02.0
Texas3.04.05.0
Colorado6.07.08.0
In [104]:
df2
Out[104]:
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.0
In [105]:
df1 + df2 # 空值用NaN代替
Out[105]:
bcde
ColoradoNaNNaNNaNNaN
Ohio3.0NaN6.0NaN
OregonNaNNaNNaNNaN
Texas9.0NaN12.0NaN
UtahNaNNaNNaNNaN

在算数方法中填充值

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]:
abcd
00.01.02.03.0
14.05.06.07.0
28.09.010.011.0
In [107]:
df2
Out[107]:
abcde
00.01.02.03.04.0
15.06.07.08.09.0
210.011.012.013.014.0
315.016.017.018.019.0
In [108]:
df1 + df2
Out[108]:
abcde
00.02.04.06.0NaN
19.011.013.015.0NaN
218.020.022.024.0NaN
3NaNNaNNaNNaNNaN
In [109]:
df1.add(df2, fill_value=0)
Out[109]:
abcde
00.02.04.06.04.0
19.011.013.015.09.0
218.020.022.024.014.0
315.016.017.018.019.0
In [110]:
df1.reindex(columns=df2.columns, fill_value=0)
Out[110]:
abcde
00.01.02.03.00
14.05.06.07.00
28.09.010.011.00

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]:
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.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]:
bde
Utah0.00.00.0
Ohio3.03.03.0
Texas6.06.06.0
Oregon9.09.09.0
In [117]:
series2 = Series(range(3), index=['b', 'e', 'f'])
frame + series2
Out[117]:
bdef
Utah0.0NaN3.0NaN
Ohio3.0NaN6.0NaN
Texas6.0NaN9.0NaN
Oregon9.0NaN12.0NaN
In [118]:
series3 = frame['d']
frame
Out[118]:
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.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]:
bde
Utah-1.00.01.0
Ohio-1.00.01.0
Texas-1.00.01.0
Oregon-1.00.01.0

函数应用和映射

In [121]:
frame = DataFrame(np.random.randn(4, 3), columns=list('bde'),
                  index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [122]:
frame
Out[122]:
bde
Utah-0.2047080.478943-0.519439
Ohio-0.5557301.9657811.393406
Texas0.0929080.2817460.769023
Oregon1.2464351.007189-1.296221
In [123]:
np.abs(frame) #求绝对值
Out[123]:
bde
Utah0.2047080.4789430.519439
Ohio0.5557301.9657811.393406
Texas0.0929080.2817460.769023
Oregon1.2464351.0071891.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]:
bde
min-0.5557300.281746-1.296221
max1.2464351.9657811.393406
In [128]:
format = lambda x: '%.2f' % x
frame.applymap(format)
Out[128]:
bde
Utah-0.200.48-0.52
Ohio-0.561.971.39
Texas0.090.280.77
Oregon1.251.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]:
dabc
one4567
three0123
In [133]:
frame.sort_index(axis=1)
Out[133]:
abcd
three1230
one5674
In [134]:
frame.sort_index(axis=1, ascending=False)
Out[134]:
dcba
three0321
one4765
In [137]:
frame = DataFrame({'b': [4, 7, -3, 2], 'a': [0, 1, 0, 1]})
frame
Out[137]:
ab
004
117
20-3
312
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]:
ab
20-3
312
004
117
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]:
ab
20-3
004
312
117
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]:
abc
004.3-2.0
117.05.0
20-3.08.0
312.0-2.5
In [144]:
frame.rank(axis=1)
Out[144]:
abc
02.03.01.0
11.03.02.0
22.01.03.0
32.03.01.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]:
012
a0.2749920.2289131.352917
a0.886429-2.001637-0.371843
b1.669025-0.438570-0.539741
b0.4769853.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]:
012
b1.669025-0.438570-0.539741
b0.4769853.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]:
onetwo
a1.40NaN
b7.10-4.5
cNaNNaN
d0.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]:
onetwo
a1.40NaN
b8.50-4.5
cNaNNaN
d9.25-5.8
In [157]:
df.describe() #汇总多个统计数据
Out[157]:
onetwo
count3.0000002.000000
mean3.083333-2.900000
std3.4936852.262742
min0.750000-4.500000
25%1.075000-3.700000
50%1.400000-2.900000
75%4.250000-2.100000
max7.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]:
Qu1Qu2Qu3
0121
1335
2412
3324
4434
In [167]:
result = data.apply(pd.value_counts).fillna(0)
result
Out[167]:
Qu1Qu2Qu3
11.01.01.0
20.02.01.0
32.02.00.0
42.00.02.0
50.00.01.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]:
012
01.06.53.0
11.0NaNNaN
2NaNNaNNaN
3NaN6.53.0
In [174]:
cleaned
Out[174]:
012
01.06.53.0
In [175]:
data.dropna(how='all') # 只干掉全为na的行
Out[175]:
012
01.06.53.0
11.0NaNNaN
3NaN6.53.0
In [176]:
data[4] = NA
data
Out[176]:
0124
01.06.53.0NaN
11.0NaNNaNNaN
2NaNNaNNaNNaN
3NaN6.53.0NaN
In [178]:
data.dropna(axis=1, how='all') # axis = 1,干掉全为NA的列
Out[178]:
012
01.06.53.0
11.0NaNNaN
2NaNNaNNaN
3NaN6.53.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]:
012
0-0.577087NaNNaN
10.523772NaNNaN
2-0.713544NaNNaN
3-1.860761NaN0.560145
4-1.265934NaN-1.063512
50.332883-2.359419-0.199543
6-1.541996-0.970736-1.307030
In [180]:
df.dropna(thresh=3) #留一部分观测数据
Out[180]:
012
50.332883-2.359419-0.199543
6-1.541996-0.970736-1.307030

填充缺失数据

In [181]:
df.fillna(0) #用0来填充缺失的数据
Out[181]:
012
0-0.5770870.0000000.000000
10.5237720.0000000.000000
2-0.7135440.0000000.000000
3-1.8607610.0000000.560145
4-1.2659340.000000-1.063512
50.332883-2.359419-0.199543
6-1.541996-0.970736-1.307030
In [182]:
df.fillna({1: 0.5, 3: -1})
Out[182]:
012
0-0.5770870.500000NaN
10.5237720.500000NaN
2-0.7135440.500000NaN
3-1.8607610.5000000.560145
4-1.2659340.500000-1.063512
50.332883-2.359419-0.199543
6-1.541996-0.970736-1.307030
In [185]:
# 通常会返回新对象,但也可以对现有对象进行就地修改
_ = df.fillna(0, inplace=True)
df
Out[185]:
012
00.2863500.377984-0.753887
10.3312861.3497420.069877
20.2466740.0000001.004812
31.3271950.000000-1.549106
40.0221850.0000000.000000
50.8625800.0000000.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]:
012
00.6702160.852965-0.955869
1-0.023493-2.304234-0.652469
2-1.218302NaN1.074623
30.723642NaN1.001543
4-0.503087NaNNaN
5-0.726213NaNNaN
In [186]:
df.fillna(method='ffill')
Out[186]:
012
00.2863500.377984-0.753887
10.3312861.3497420.069877
20.2466740.0000001.004812
31.3271950.000000-1.549106
40.0221850.0000000.000000
50.8625800.0000000.000000
In [188]:
df.fillna(method='ffill', limit=2)
Out[188]:
012
00.6702160.852965-0.955869
1-0.023493-2.304234-0.652469
2-1.218302-2.3042341.074623
30.723642-2.3042341.001543
4-0.503087NaN1.001543
5-0.726213NaN1.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]:
123
a-1.1577190.8167070.433610
b1.0107371.824875-0.997518
c0.850591-0.131578NaN
dNaN0.9124140.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]:
OhioColorado
GreenRedGreen
a1012
2345
b1678
291011
In [199]:
frame.index.names = ['key1', 'key2']
frame.columns.names = ['state', 'color']
frame
Out[199]:
stateOhioColorado
colorGreenRedGreen
key1key2
a1012
2345
b1678
291011
In [200]:
frame['Ohio']
Out[200]:
colorGreenRed
key1key2
a101
234
b167
2910

重排分级顺序

In [202]:
frame.swaplevel('key1', 'key2')
Out[202]:
stateOhioColorado
colorGreenRedGreen
key2key1
1a012
2a345
1b678
2b91011
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]:
stateOhioColorado
colorGreenRedGreen
key1key2
a1012
b1678
a2345
b291011
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]:
stateOhioColorado
colorGreenRedGreen
key2key1
1a012
b678
2a345
b91011

根据级别汇总统计

In [205]:
frame.sum(level='key2')
Out[205]:
stateOhioColorado
colorGreenRedGreen
key2
16810
2121416
In [206]:
frame.sum(level='color', axis=1)
Out[206]:
colorGreenRed
key1key2
a121
284
b1147
22010

使用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]:
abcd
007one0
116one1
225one2
334two0
443two1
552two2
661two3
In [208]:
frame2 = frame.set_index(['c', 'd'])
frame2
Out[208]:
ab
cd
one007
116
225
two034
143
252
361
In [209]:
frame.set_index(['c', 'd'], drop=False)
Out[209]:
abcd
cd
one007one0
116one1
225one2
two034two0
143two1
252two2
361two3
In [210]:
frame2.reset_index()
Out[210]:
cdab
0one007
1one116
2one225
3two034
4two143
5two252
6two361

其他关于 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若云流风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值