當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas Series.set_value()用法及代碼示例

Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。

Pandas Series.set_value()函數用於使用索引標簽設置給定係列對象的值。

用法: Series.set_value(label, value, takeable=False)

參數:
label:不允許使用MultiIndex進行部分索引
value:標量值
takeable:將索引解釋為索引器,默認為False

返回:係列

範例1:采用Series.set_value()函數使用索引標簽設置給定係列對象中的值。

# 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)

輸出:

現在我們將使用Series.set_value()函數設置與傳遞的索引標簽相對應的值。

# set the value 
sr.set_value('City 2', 'Dublin')

輸出:


正如我們在輸出中看到的,Series.set_value()函數已成功設置傳遞的索引標簽的值。

範例2:采用Series.set_value()函數使用索引標簽設置給定係列對象中的值。

# 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)

輸出:

現在我們將使用Series.set_value()函數設置給定係列對象中的值。

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

輸出:

正如我們在輸出中看到的,Series.set_value()函數已成功設置傳遞的索引標簽的值。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.set_value()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。