Python | Pandas Series.set_value()
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 FalseReturns : series
Example #1: Use Series.set_value() function to set the value in the given series object using the index labels.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow']) # Create the Indexindex_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6'] # set the indexsr.index = index_ # Print the seriesprint(sr) |
Output :

Now we will use Series.set_value() function to set the value corresponding to the passed index label.
# set the valuesr.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 pdimport pandas as pd # Creating the Seriessr = pd.Series([100, 25, 32, 118, 24, 65]) # Print the seriesprint(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 labelsr.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.





Please Login to comment...