Modifying a Series in-place
In-place modification of a Series
is a slightly controversial topic. When possible, it is preferred to perform operations that return a new Series
with the modifications represented in the new Series
. But, if needed, it is possible to change values and add/remove rows in-place.
An additional row can be added in place to a series by assigning a value to an index
label that does not already exist. The following code creates a Series
object and adds an additional item to the series:


The value at a specific index label can be changed in place by assignment:

Rows can be removed from a Series
by passing their index
labels to the del()
function. The following demonstrates removal of the row with the index label 'a'
:

Note
To add and remove items out of place, you use pd.concat()
to add and remove using a Boolean selection.
An important thing to keep in mind when using slicing is that the result of the slice is a view into the original Series
. Modification of values through...