放大设置

The .loc/[] operations can perform enlargement when setting a non-existent key for that axis.

In the Series case this is effectively an appending operation.

  1. In [130]: se = pd.Series([1,2,3])
  2. In [131]: se
  3. Out[131]:
  4. 0 1
  5. 1 2
  6. 2 3
  7. dtype: int64
  8. In [132]: se[5] = 5.
  9. In [133]: se
  10. Out[133]:
  11. 0 1.0
  12. 1 2.0
  13. 2 3.0
  14. 5 5.0
  15. dtype: float64

A DataFrame can be enlarged on either axis via .loc.

  1. In [134]: dfi = pd.DataFrame(np.arange(6).reshape(3,2),
  2. .....: columns=['A','B'])
  3. .....:
  4. In [135]: dfi
  5. Out[135]:
  6. A B
  7. 0 0 1
  8. 1 2 3
  9. 2 4 5
  10. In [136]: dfi.loc[:,'C'] = dfi.loc[:,'A']
  11. In [137]: dfi
  12. Out[137]:
  13. A B C
  14. 0 0 1 0
  15. 1 2 3 2
  16. 2 4 5 4

This is like an append operation on the DataFrame.

  1. In [138]: dfi.loc[3] = 5
  2. In [139]: dfi
  3. Out[139]:
  4. A B C
  5. 0 0 1 0
  6. 1 2 3 2
  7. 2 4 5 4
  8. 3 5 5 5