2.3 Attribute Access

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

In [1]: sa
Out[1]: 
a    1
b    2
c    3
dtype: int64

In [2]: sa.b
Out[2]: 2

In [3]: dfa
Out[3]: 
                 A       B       C       D
2000-01-01  0.4691 -0.2829 -1.5091 -1.1356
2000-01-02  1.2121 -0.1732  0.1192 -1.0442
2000-01-03 -0.8618 -2.1046 -0.4949  1.0718
2000-01-04  0.7216 -0.7068 -1.0396  0.2719
2000-01-05 -0.4250  0.5670  0.2762 -1.0874
2000-01-06 -0.6737  0.1136 -1.4784  0.5250
2000-01-07  0.4047  0.5770 -1.7150 -1.0393
2000-01-08 -0.3706 -1.1579 -1.3443  0.8449

In [4]: dfa.A
Out[4]: 
2000-01-01    0.4691
2000-01-02    1.2121
2000-01-03   -0.8618
2000-01-04    0.7216
2000-01-05   -0.4250
2000-01-06   -0.6737
2000-01-07    0.4047
2000-01-08   -0.3706
Freq: D, Name: A, dtype: float64

In [5]: panel
Out[5]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 8 (major_axis) x 4 (minor_axis)
Items axis: one to two
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-08 00:00:00
Minor_axis axis: A to D

In [6]: panel.one
Out[6]: 
                 A       B       C       D
2000-01-01  0.4691 -0.2829 -1.5091 -1.1356
2000-01-02  1.2121 -0.1732  0.1192 -1.0442
2000-01-03 -0.8618 -2.1046 -0.4949  1.0718
2000-01-04  0.7216 -0.7068 -1.0396  0.2719
2000-01-05 -0.4250  0.5670  0.2762 -1.0874
2000-01-06 -0.6737  0.1136 -1.4784  0.5250
2000-01-07  0.4047  0.5770 -1.7150 -1.0393
2000-01-08 -0.3706 -1.1579 -1.3443  0.8449

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 fails silently, creating a new attribute rather than a new column.

In [7]: sa
Out[7]: 
a    1
b    2
c    3
dtype: int64

In [8]: sa.a = 5

In [9]: sa
Out[9]: 
a    5
b    2
c    3
dtype: int64

In [10]: dfa
Out[10]: 
                 A       B       C       D
2000-01-01  0.4691 -0.2829 -1.5091 -1.1356
2000-01-02  1.2121 -0.1732  0.1192 -1.0442
2000-01-03 -0.8618 -2.1046 -0.4949  1.0718
2000-01-04  0.7216 -0.7068 -1.0396  0.2719
2000-01-05 -0.4250  0.5670  0.2762 -1.0874
2000-01-06 -0.6737  0.1136 -1.4784  0.5250
2000-01-07  0.4047  0.5770 -1.7150 -1.0393
2000-01-08 -0.3706 -1.1579 -1.3443  0.8449

In [11]: dfa.A = list(range(len(dfa.index)))  # ok if A already exists

In [12]: dfa
Out[12]: 
            A       B       C       D
2000-01-01  0 -0.2829 -1.5091 -1.1356
2000-01-02  1 -0.1732  0.1192 -1.0442
2000-01-03  2 -2.1046 -0.4949  1.0718
2000-01-04  3 -0.7068 -1.0396  0.2719
2000-01-05  4  0.5670  0.2762 -1.0874
2000-01-06  5  0.1136 -1.4784  0.5250
2000-01-07  6  0.5770 -1.7150 -1.0393
2000-01-08  7 -1.1579 -1.3443  0.8449

In [13]: dfa['A'] = list(range(len(dfa.index)))  # use this form to create a new column

In [14]: dfa
Out[14]: 
            A       B       C       D
2000-01-01  0 -0.2829 -1.5091 -1.1356
2000-01-02  1 -0.1732  0.1192 -1.0442
2000-01-03  2 -2.1046 -0.4949  1.0718
2000-01-04  3 -0.7068 -1.0396  0.2719
2000-01-05  4  0.5670  0.2762 -1.0874
2000-01-06  5  0.1136 -1.4784  0.5250
2000-01-07  6  0.5770 -1.7150 -1.0393
2000-01-08  7 -1.1579 -1.3443  0.8449

Warning

  • 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, labels.
  • 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.
  • The Series/Panel accesses are available starting in 0.13.0.

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:

In [15]: x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]})

In [16]: x
Out[16]: 
   x  y
0  1  3
1  2  4
2  3  5

In [17]: x.iloc[1] = dict(x=9, y=99)

In [18]: x
Out[18]: 
   x   y
0  1   3
1  9  99
2  3   5