Sunday, December 29, 2024
Google search engine
HomeLanguagesPython | Pandas Series.set_value()

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 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.

RELATED ARTICLES

Most Popular

Recent Comments