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

เขียนโค้ด Python รวมสอง Series เป็น Series เดียวโดยไม่ซ้ำดัชนี


อินพุต - สมมติคุณมี series และผลลัพธ์การ concat โดยไม่ซ้ำดัชนีคือ

0    1 1    2 2    3 3    4 4    5 5    6

วิธีแก้ปัญหา

ทำตาม 2 ขั้นตอน

  • สร้างสอง Series

  • ใช้ pd.concat กับ ignore_index=True

pd.concat([series_one,series_two],ignore_index=True)

ตัวอย่าง

ดูโค้ดด้านล่าง

import pandas as pd
series_one = pd.Series([1,2,3])
series_two = pd.Series([4,5,6])
print(pd.concat([series_one,series_two],ignore_index=True))

ผลลัพธ์

0    1 1    2 2    3 3    4 4    5 5    6