Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเพิ่มองค์ประกอบ (Append Elements) ให้กับ Pandas Series


ในโปรแกรมนี้ เราจะเพิ่มองค์ประกอบให้กับ Pandas Series เราจะใช้ฟังก์ชัน append() สำหรับงานนี้ โปรดทราบว่าเราสามารถ append ได้เฉพาะ Series หรือ list/tuple ของ Series เท่านั้น

อัลกอริทึม

ขั้นตอนที่ 1: กำหนด Pandas Series คือ s1
ขั้นตอนที่ 2: กำหนด Series อีกตัวคือ s2
ขั้นตอนที่ 3: Append s2 เข้าไปใน s1
ขั้นตอนที่ 4: แสดง Series ที่ append แล้ว

ตัวอย่างโค้ด

import pandas as pd
s1 = pd.Series([10,20,30,40,50])
s2 = pd.Series([11,22,33,44,55])
print("S1:\n",s1)
print("\nS2:\n",s2)
appended_series = s1.append(s2)
print("\nFinal Series after appending:\n",appended_series)

ผลลัพธ์

S1:
0    10
1    20
2    30
3    40
4    50
dtype: int64
S2:
0    11
1    22
2    33
3    44
4    55
dtype: int64
Final Series after appending:
0    10
1    20
2    30
3    40
4    50
0    11
1    22
2    33
3    44
4    55
dtype: int64