Series เป็นโครงสร้างข้อมูล 1 มิติ มี label (index) ใน Pandas library สามารถเก็บข้อมูลชนิดใดก็ได้: integer, float, string, python objects สร้างได้จาก array, dictionary, หรือค่าคงที่
ตัวอย่าง: สร้าง Series ว่าง
import pandas as pd
my_series = pd.Series()
print("This is an empty series data structure")
print(my_series)
ผลลัพธ์
This is an empty series data structure Series([], dtype: float64)
คำอธิบาย
- นำเข้า pandas ชื่อ pd
- สร้าง Series ด้วยฟังก์ชัน Series()
- พิมพ์ผลลัพธ์
ตัวอย่าง: สร้าง Series จาก Numpy Array โดยไม่ระบุ index
import pandas as pd
import numpy as np
my_data = np.array(['ab','bc','cd','de','ef','fg','gh','hi'])
my_series = pd.Series(my_data)
print("This is series data structure created using Numpy array")
print(my_series)
ผลลัพธ์
This is series data structure created using Numpy array 0 ab 1 bc 2 cd 3 de 4 ef 5 fg 6 gh 7 hi dtype: object
คำอธิบาย
- นำเข้าไลบรารี ตั้ง alias
- สร้าง numpy array เก็บข้อมูล
- สร้าง Series ส่งข้อมูลเป็นพารามิเตอร์
- แสดงผล
หมายเหตุ ถ้าไม่ระบุ index จะใช้ค่า default 0, 1, 2, ...