Python | Pandas Series.set_value()
Last Updated :
05 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.set_value()
function is used to set value of the given series object using the index labels.
Syntax: Series.set_value(label, value, takeable=False)
Parameter :
label : Partial indexing with MultiIndex not allowed
value : Scalar value
takeable : interpret the index as indexers, default False
Returns : series
Example #1: Use Series.set_value()
function to set the value in the given series object using the index labels.
import pandas as pd
sr = pd.Series([ 'New York' , 'Chicago' , 'Toronto' , 'Lisbon' , 'Rio' , 'Moscow' ])
index_ = [ 'City 1' , 'City 2' , 'City 3' , 'City 4' , 'City 5' , 'City 6' ]
sr.index = index_
print (sr)
|
Output :
Now we will use Series.set_value()
function to set the value corresponding to the passed index label.
sr.set_value( 'City 2' , 'Dublin' )
|
Output :
As we can see in the output, the Series.set_value()
function has successfully set the value of the passed index label.
Example #2: Use Series.set_value()
function to set the value in the given series object using the index labels.
import pandas as pd
sr = pd.Series([ 100 , 25 , 32 , 118 , 24 , 65 ])
print (sr)
|
Output :
Now we will use Series.set_value()
function to set the value in the given series object.
Output :
As we can see in the output, the Series.set_value()
function has successfully set the value of the passed index label.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...