Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách thêm phần tử vào Pandas Series


Trong chương trình này, chúng ta sẽ thêm phần tử vào Pandas series. Dùng hàm append(). Lưu ý: chỉ có thể append một series hoặc list/tuple của series vào series hiện có.

Thuật toán

Bước 1: Định nghĩa Pandas series s1.
Bước 2: Định nghĩa series s2.
Bước 3: Append s2 vào s1.
Bước 4: In series sau khi append.

Mã ví dụ

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("\nSeries sau khi append:\n", appended_series)

Kết quả

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
Series sau khi append:
0    10
1    20
2    30
3    40
4    50
0    11
1    22
2    33
3    44
4    55
dtype: int64