属性访问

You may access an index on a Series, column on a DataFrame, and an item on a Panel directly as an attribute:

  1. In [17]: sa = pd.Series([1,2,3],index=list('abc'))
  2. In [18]: dfa = df.copy()
  1. In [19]: sa.b
  2. Out[19]: 2
  3. In [20]: dfa.A
  4. Out[20]:
  5. 2000-01-01 0.469112
  6. 2000-01-02 1.212112
  7. 2000-01-03 -0.861849
  8. 2000-01-04 0.721555
  9. 2000-01-05 -0.424972
  10. 2000-01-06 -0.673690
  11. 2000-01-07 0.404705
  12. 2000-01-08 -0.370647
  13. Freq: D, Name: A, dtype: float64
  14. In [21]: panel.one
  15. Out[21]:
  16. A B C D
  17. 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632
  18. 2000-01-02 1.212112 -0.173215 0.119209 -1.044236
  19. 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804
  20. 2000-01-04 0.721555 -0.706771 -1.039575 0.271860
  21. 2000-01-05 -0.424972 0.567020 0.276232 -1.087401
  22. 2000-01-06 -0.673690 0.113648 -1.478427 0.524988
  23. 2000-01-07 0.404705 0.577046 -1.715002 -1.039268
  24. 2000-01-08 -0.370647 -1.157892 -1.344312 0.844885
  1. In [22]: sa.a = 5
  2. In [23]: sa
  3. Out[23]:
  4. a 5
  5. b 2
  6. c 3
  7. dtype: int64
  8. In [24]: dfa.A = list(range(len(dfa.index))) # ok if A already exists
  9. In [25]: dfa
  10. Out[25]:
  11. A B C D
  12. 2000-01-01 0 -0.282863 -1.509059 -1.135632
  13. 2000-01-02 1 -0.173215 0.119209 -1.044236
  14. 2000-01-03 2 -2.104569 -0.494929 1.071804
  15. 2000-01-04 3 -0.706771 -1.039575 0.271860
  16. 2000-01-05 4 0.567020 0.276232 -1.087401
  17. 2000-01-06 5 0.113648 -1.478427 0.524988
  18. 2000-01-07 6 0.577046 -1.715002 -1.039268
  19. 2000-01-08 7 -1.157892 -1.344312 0.844885
  20. In [26]: dfa['A'] = list(range(len(dfa.index))) # use this form to create a new column
  21. In [27]: dfa
  22. Out[27]:
  23. A B C D
  24. 2000-01-01 0 -0.282863 -1.509059 -1.135632
  25. 2000-01-02 1 -0.173215 0.119209 -1.044236
  26. 2000-01-03 2 -2.104569 -0.494929 1.071804
  27. 2000-01-04 3 -0.706771 -1.039575 0.271860
  28. 2000-01-05 4 0.567020 0.276232 -1.087401
  29. 2000-01-06 5 0.113648 -1.478427 0.524988
  30. 2000-01-07 6 0.577046 -1.715002 -1.039268
  31. 2000-01-08 7 -1.157892 -1.344312 0.844885

警告

  • You can use this access only if the index element is a valid Python identifier, e.g. s.1 is not allowed. See here for an explanation of valid identifiers.
  • The attribute will not be available if it conflicts with an existing method name, e.g. s.min is not allowed.
  • Similarly, the attribute will not be available if it conflicts with any of the following list: index, major_axis, minor_axis, items.
  • In any of these cases, standard indexing will still work, e.g. s[‘1’], s[‘min’], and s[‘index’] will access the corresponding element or column.

If you are using the IPython environment, you may also use tab-completion to see these accessible attributes.

You can also assign a dict to a row of a DataFrame:

  1. In [28]: x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]})
  2. In [29]: x.iloc[1] = dict(x=9, y=99)
  3. In [30]: x
  4. Out[30]:
  5. x y
  6. 0 1 3
  7. 1 9 99
  8. 2 3 5

You can use attribute access to modify an existing element of a Series or column of a DataFrame, but be careful; if you try to use attribute access to create a new column, it creates a new attribute rather than a new column. In 0.21.0 and later, this will raise a UserWarning:

  1. In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
  2. In[2]: df.two = [4, 5, 6]
  3. UserWarning: Pandas doesn't allow Series to be assigned into nonexistent columns - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute_access
  4. In[3]: df
  5. Out[3]:
  6. one
  7. 0 1.0
  8. 1 2.0
  9. 2 3.0