Open In App

Python | Pandas Series.set_value()

Last Updated : 05 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
  
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6']
  
# set the index
sr.index = index_
  
# Print the series
print(sr)


Output :

Now we will use Series.set_value() function to set the value corresponding to the passed index label.




# set the value
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.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([100, 25, 32, 118, 24, 65])
  
# Print the series
print(sr)


Output :

Now we will use Series.set_value() function to set the value in the given series object.




# set the value to 1000 of
# the passed index label
sr.set_value(3, 1000)


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

Similar Reads