2.9 Setting With Enlargement
New in version 0.13.
The .loc/.ix/[]
operations can perform enlargement when setting a non-existant key for that axis.
In the Series
case this is effectively an appending operation
In [1]: se = pd.Series([1,2,3])
In [2]: se
Out[2]:
0 1
1 2
2 3
dtype: int64
In [3]: se[5] = 5.
In [4]: se
Out[4]:
0 1.0
1 2.0
2 3.0
5 5.0
dtype: float64
A DataFrame
can be enlarged on either axis via .loc
In [5]: dfi = pd.DataFrame(np.arange(6).reshape(3,2),
...: columns=['A','B'])
...:
In [6]: dfi
Out[6]:
A B
0 0 1
1 2 3
2 4 5
In [7]: dfi.loc[:,'C'] = dfi.loc[:,'A']
In [8]: dfi
Out[8]:
A B C
0 0 1 0
1 2 3 2
2 4 5 4
This is like an append
operation on the DataFrame
.
In [9]: dfi.loc[3] = 5
In [10]: dfi
Out[10]:
A B C
0 0 1 0
1 2 3 2
2 4 5 4
3 5 5 5