Computer >> Máy Tính >  >> Lập trình >> Python

Làm cách nào để nối các phần tử vào chuỗi Pandas?

Trong chương trình này, chúng tôi sẽ nối các phần tử vào một chuỗi Pandas. Chúng tôi sẽ sử dụng hàm append () cho tác vụ này. Xin lưu ý rằng chúng tôi chỉ có thể nối một bộ hoặc danh sách / nhiều bộ vào bộ hiện có.

Thuật toán

Step1: Define a Pandas series, s1.
Step 2: Define another series, s2.
Step 3: Append s2 to s1.
Step 4: Print the final appended series.

Mã mẫu

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)

Đầu ra

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