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


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


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

Pandas Series.append()函數用於連接兩個或多個係列對象。

用法: Series.append(to_append, ignore_index=False, verify_integrity=False)

參數:
to_append:係列或係列列表/元組
ignore_index:如果為True,則不要使用索引標簽。
verify_integrity:如果為True,則在創建具有重複項的索引時引發異常

返回:附加:係列

範例1:采用Series.append()函數將傳遞的係列對象附加到此係列對象的末尾。

# importing pandas as pd 
import pandas as pd 
  
# Creating the first Series 
sr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio']) 
  
# Create the first Index 
index_1 = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']  
  
# set the index of first series 
sr1.index = index_1 
  
# Creating the second Series 
sr2 = pd.Series(['Chicage', 'Shanghai', 'Beijing', 'Jakarta', 'Seoul']) 
  
# Create the second Index 
index_2 = ['City 6', 'City 7', 'City 8', 'City 9', 'City 10']  
  
# set the index of second series 
sr2.index = index_2 
  
# Print the first series 
print(sr1) 
  
# Print the second series 
print(sr2)

輸出:

City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype:object

City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype:object

現在我們將使用Series.append()函數將sr2附加到sr1係列的末尾。

# append sr2 at the end of sr1 
result = sr1.append(sr2) 
  
# Print the result 
print(result)

輸出:

City 1     New York
City 2      Chicago
City 3      Toronto
City 4       Lisbon
City 5          Rio
City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype:object

正如我們在輸出中看到的,Series.append()函數已成功將sr2對象附加到sr1對象的末尾。

範例2:采用Series.append()函數將傳遞的係列對象附加到此係列對象的末尾。忽略兩個係列對象的原始索引。

# importing pandas as pd 
import pandas as pd 
  
# Creating the first Series 
sr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio']) 
  
# Create the first Index 
index_1 = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']  
  
# set the index of first series 
sr1.index = index_1 
  
# Creating the second Series 
sr2 = pd.Series(['Chicage', 'Shanghai', 'Beijing', 'Jakarta', 'Seoul']) 
  
# Create the second Index 
index_2 = ['City 6', 'City 7', 'City 8', 'City 9', 'City 10']  
  
# set the index of second series 
sr2.index = index_2 
  
# Print the first series 
print(sr1) 
  
# Print the second series 
print(sr2)

輸出:

City 1    New York
City 2     Chicago
City 3     Toronto
City 4      Lisbon
City 5         Rio
dtype:object

City 6      Chicage
City 7     Shanghai
City 8      Beijing
City 9      Jakarta
City 10       Seoul
dtype:object

現在我們將使用Series.append()函數將sr2附加到sr1係列的末尾。我們將忽略給定係列對象的索引。

# append sr2 at the end of sr1 
# ignore the index 
result = sr1.append(sr2, ignore_index = True) 
  
# Print the result 
print(result)

輸出:

0    New York
1     Chicago
2     Toronto
3      Lisbon
4         Rio
5     Chicage
6    Shanghai
7     Beijing
8     Jakarta
9       Seoul
dtype:object

正如我們在輸出中看到的,Series.append()函數已成功將sr2對象附加到sr1對象的末尾,並且它也忽略了索引。



相關用法


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