三、 选择

虽然标准的 Python/Numpy 的选择和设置表达式都能够直接派上用场,但是作为工程使用的代码,我们推荐使用经过优化的 pandas 数据访问方式: .at, .iat, .loc, .iloc.ix。详情请参阅索引和选取数据多重索引/高级索引

获取

1、 选择一个单独的列,这将会返回一个Series,等同于df.A

  1. In [23]: df['A']
  2. Out[23]:
  3. 2013-01-01 0.469112
  4. 2013-01-02 1.212112
  5. 2013-01-03 -0.861849
  6. 2013-01-04 0.721555
  7. 2013-01-05 -0.424972
  8. 2013-01-06 -0.673690
  9. Freq: D, Name: A, dtype: float64

2、 通过[]进行选择,这将会对行进行切片

  1. In [24]: df[0:3]
  2. Out[24]:
  3. A B C D
  4. 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
  5. 2013-01-02 1.212112 -0.173215 0.119209 -1.044236
  6. 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
  7. In [25]: df['20130102':'20130104']
  8. Out[25]:
  9. A B C D
  10. 2013-01-02 1.212112 -0.173215 0.119209 -1.044236
  11. 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
  12. 2013-01-04 0.721555 -0.706771 -1.039575 0.271860

通过标签选择

1、 使用标签来获取一个交叉的区域

  1. In [26]: df.loc[dates[0]]
  2. Out[26]:
  3. A 0.469112
  4. B -0.282863
  5. C -1.509059
  6. D -1.135632
  7. Name: 2013-01-01 00:00:00, dtype: float64

2、 通过标签来在多个轴上进行选择

  1. In [27]: df.loc[:,['A','B']]
  2. Out[27]:
  3. A B
  4. 2013-01-01 0.469112 -0.282863
  5. 2013-01-02 1.212112 -0.173215
  6. 2013-01-03 -0.861849 -2.104569
  7. 2013-01-04 0.721555 -0.706771
  8. 2013-01-05 -0.424972 0.567020
  9. 2013-01-06 -0.673690 0.113648

3、 标签切片

  1. In [28]: df.loc['20130102':'20130104',['A','B']]
  2. Out[28]:
  3. A B
  4. 2013-01-02 1.212112 -0.173215
  5. 2013-01-03 -0.861849 -2.104569
  6. 2013-01-04 0.721555 -0.706771

4、 对于返回的对象进行维度缩减

  1. In [29]: df.loc['20130102',['A','B']]
  2. Out[29]:
  3. A 1.212112
  4. B -0.173215
  5. Name: 2013-01-02 00:00:00, dtype: float64

5、 获取一个标量

  1. In [30]: df.loc[dates[0],'A']
  2. Out[30]: 0.46911229990718628

6、 快速访问一个标量(与上一个方法等价)

  1. In [31]: df.at[dates[0],'A']
  2. Out[31]: 0.46911229990718628

通过位置选择

1、 通过传递数值进行位置选择(选择的是行)

  1. In [32]: df.iloc[3]
  2. Out[32]:
  3. A 0.721555
  4. B -0.706771
  5. C -1.039575
  6. D 0.271860
  7. Name: 2013-01-04 00:00:00, dtype: float64

2、 通过数值进行切片,与 numpy/python 中的情况类似

  1. In [33]: df.iloc[3:5,0:2]
  2. Out[33]:
  3. A B
  4. 2013-01-04 0.721555 -0.706771
  5. 2013-01-05 -0.424972 0.567020

3、 通过指定一个位置的列表,与 numpy/python 中的情况类似

  1. In [34]: df.iloc[[1,2,4],[0,2]]
  2. Out[34]:
  3. A C
  4. 2013-01-02 1.212112 0.119209
  5. 2013-01-03 -0.861849 -0.494929
  6. 2013-01-05 -0.424972 0.276232

4、 对行进行切片

  1. In [35]: df.iloc[1:3,:]
  2. Out[35]:
  3. A B C D
  4. 2013-01-02 1.212112 -0.173215 0.119209 -1.044236
  5. 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804

5、 对列进行切片

  1. In [36]: df.iloc[:,1:3]
  2. Out[36]:
  3. B C
  4. 2013-01-01 -0.282863 -1.509059
  5. 2013-01-02 -0.173215 0.119209
  6. 2013-01-03 -2.104569 -0.494929
  7. 2013-01-04 -0.706771 -1.039575
  8. 2013-01-05 0.567020 0.276232
  9. 2013-01-06 0.113648 -1.478427

6、 获取特定的值

  1. In [37]: df.iloc[1,1]
  2. Out[37]: -0.17321464905330858

快速访问标量(等同于前一个方法):

  1. In [38]: df.iat[1,1]
  2. Out[38]: -0.17321464905330858

布尔索引

1、 使用一个单独列的值来选择数据:

  1. In [39]: df[df.A > 0]
  2. Out[39]:
  3. A B C D
  4. 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
  5. 2013-01-02 1.212112 -0.173215 0.119209 -1.044236
  6. 2013-01-04 0.721555 -0.706771 -1.039575 0.271860

2、 使用where操作来选择数据:

  1. In [40]: df[df > 0]
  2. Out[40]:
  3. A B C D
  4. 2013-01-01 0.469112 NaN NaN NaN
  5. 2013-01-02 1.212112 NaN 0.119209 NaN
  6. 2013-01-03 NaN NaN NaN 1.071804
  7. 2013-01-04 0.721555 NaN NaN 0.271860
  8. 2013-01-05 NaN 0.567020 0.276232 NaN
  9. 2013-01-06 NaN 0.113648 NaN 0.524988

3、 使用isin()方法来过滤:

  1. In [41]: df2 = df.copy()
  2. In [42]: df2['E'] = ['one', 'one','two','three','four','three']
  3. In [43]: df2
  4. Out[43]:
  5. A B C D E
  6. 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 one
  7. 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 one
  8. 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 two
  9. 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 three
  10. 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 four
  11. 2013-01-06 -0.673690 0.113648 -1.478427 0.524988 three
  12. In [44]: df2[df2['E'].isin(['two','four'])]
  13. Out[44]:
  14. A B C D E
  15. 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 two
  16. 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 four

设置

1、 设置一个新的列:

  1. In [45]: s1 = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6))
  2. In [46]: s1
  3. Out[46]:
  4. 2013-01-02 1
  5. 2013-01-03 2
  6. 2013-01-04 3
  7. 2013-01-05 4
  8. 2013-01-06 5
  9. 2013-01-07 6
  10. Freq: D, dtype: int64
  11. In [47]: df['F'] = s1

2、 通过标签设置新的值:

  1. In [48]: df.at[dates[0],'A'] = 0

3、 通过位置设置新的值:

  1. In [49]: df.iat[0,1] = 0

4、 通过一个numpy数组设置一组新值:

  1. In [50]: df.loc[:,'D'] = np.array([5] * len(df))

上述操作结果如下:

  1. In [51]: df
  2. Out[51]:
  3. A B C D F
  4. 2013-01-01 0.000000 0.000000 -1.509059 5 NaN
  5. 2013-01-02 1.212112 -0.173215 0.119209 5 1.0
  6. 2013-01-03 -0.861849 -2.104569 -0.494929 5 2.0
  7. 2013-01-04 0.721555 -0.706771 -1.039575 5 3.0
  8. 2013-01-05 -0.424972 0.567020 0.276232 5 4.0
  9. 2013-01-06 -0.673690 0.113648 -1.478427 5 5.0

5、 通过where操作来设置新的值:

  1. In [52]: df2 = df.copy()
  2. In [53]: df2[df2 > 0] = -df2
  3. In [54]: df2
  4. Out[54]:
  5. A B C D F
  6. 2013-01-01 0.000000 0.000000 -1.509059 -5 NaN
  7. 2013-01-02 -1.212112 -0.173215 -0.119209 -5 -1.0
  8. 2013-01-03 -0.861849 -2.104569 -0.494929 -5 -2.0
  9. 2013-01-04 -0.721555 -0.706771 -1.039575 -5 -3.0
  10. 2013-01-05 -0.424972 -0.567020 -0.276232 -5 -4.0
  11. 2013-01-06 -0.673690 -0.113648 -1.478427 -5 -5.0