แนะนำ DataFrame

การจัดการข้อมูลด้วย pandas

Richie Cotton

Data Evangelist at DataCamp

pandas มีประโยชน์อย่างไร?

การจัดการข้อมูลด้วย pandas

โครงร่างคอร์ส

  • บทที่ 1: DataFrame
    • การเรียงลำดับและการคัดกรอง
    • การสร้างคอลัมน์ใหม่
  • บทที่ 2: การรวมข้อมูล
    • สถิติสรุป
    • การนับ
    • สถิติสรุปแบบกลุ่ม
  • บทที่ 3: การสไลซ์และการทำดัชนีข้อมูล

    • การคัดกรองด้วยการสไลซ์
    • ดัชนีและการคัดกรองด้วยดัชนี
  • บทที่ 4: การสร้างและการแสดงข้อมูล

    • การพล็อตกราฟ
    • การจัดการข้อมูลที่หายไป
    • การอ่านข้อมูลเข้าสู่ DataFrame
การจัดการข้อมูลด้วย pandas

pandas สร้างบน NumPy และ Matplotlib

รูปสามเหลี่ยมวางอยู่บนสี่เหลี่ยมสองอัน สามเหลี่ยมแทน pandas ส่วนสี่เหลี่ยมแทน NumPy และ matplotlib

การจัดการข้อมูลด้วย pandas

pandas เป็นที่นิยม

อีโมจิแพนด้าที่ปรากฏซ้ำหลายร้อยครั้ง แสดงถึงจำนวนผู้ใช้ pandas ที่มากมาย

1 https://pypistats.org/packages/pandas
การจัดการข้อมูลด้วย pandas

ข้อมูลแบบตาราง

ชื่อ สายพันธุ์ สี ส่วนสูง (ซม.) น้ำหนัก (กก.) วันเกิด
Bella Labrador Brown 56 25 2013-07-01
Charlie Poodle Black 43 23 2016-09-16
Lucy Chow Chow Brown 46 22 2014-08-25
Cooper Schnauzer Gray 49 17 2011-12-11
Max Labrador Black 59 29 2017-01-20
Stella Chihuahua Tan 18 2 2015-04-20
Bernie St. Bernard White 77 74 2018-02-27
การจัดการข้อมูลด้วย pandas

pandas DataFrame

print(dogs)
      name        breed  color  height_cm  weight_kg date_of_birth
0    Bella     Labrador  Brown         56         24    2013-07-01
1  Charlie       Poodle  Black         43         24    2016-09-16
2     Lucy    Chow Chow  Brown         46         24    2014-08-25
3   Cooper    Schnauzer   Gray         49         17    2011-12-11
4      Max     Labrador  Black         59         29    2017-01-20
5   Stella    Chihuahua    Tan         18          2    2015-04-20
6   Bernie  St. Bernard  White         77         74    2018-02-27
การจัดการข้อมูลด้วย pandas

สำรวจ DataFrame: .head()

print(dogs.head())
      name        breed  color  height_cm  weight_kg date_of_birth
0    Bella     Labrador  Brown         56         24    2013-07-01
1  Charlie       Poodle  Black         43         24    2016-09-16
2     Lucy    Chow Chow  Brown         46         24    2014-08-25
3   Cooper    Schnauzer   Gray         49         17    2011-12-11
4      Max     Labrador  Black         59         29    2017-01-20
การจัดการข้อมูลด้วย pandas

สำรวจ DataFrame: .info()

print(dogs.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7 entries, 0 to 6
Data columns (total 6 columns):
 #   Column         Non-Null Count  Dtype 
 --  ------         --------------  ----- 
 0   name           7 non-null      object
 1   breed          7 non-null      object
 2   color          7 non-null      object
 3   height_cm      7 non-null      int64 
 4   weight_kg      7 non-null      int64 
 5   date_of_birth  7 non-null      object
dtypes: int64(2), object(4)
memory usage: 464.0+ bytes
การจัดการข้อมูลด้วย pandas

สำรวจ DataFrame: .shape

print(dogs.shape)
(7, 6)
การจัดการข้อมูลด้วย pandas

สำรวจ DataFrame: .describe()

print(dogs.describe())
       height_cm  weight_kg
count   7.000000   7.000000
mean   49.714286  27.428571
std    17.960274  22.292429
min    18.000000   2.000000
25%    44.500000  19.500000
50%    49.000000  23.000000
75%    57.500000  27.000000
max    77.000000  74.000000
การจัดการข้อมูลด้วย pandas

องค์ประกอบของ DataFrame: .to_numpy()

print(dogs.to_numpy())
array([['Bella', 'Labrador', 'Brown', 56, 24, '2013-07-01'],
       ['Charlie', 'Poodle', 'Black', 43, 24, '2016-09-16'],
       ['Lucy', 'Chow Chow', 'Brown', 46, 24, '2014-08-25'],
       ['Cooper', 'Schnauzer', 'Gray', 49, 17, '2011-12-11'],
       ['Max', 'Labrador', 'Black', 59, 29, '2017-01-20'],
       ['Stella', 'Chihuahua', 'Tan', 18, 2, '2015-04-20'],
       ['Bernie', 'St. Bernard', 'White', 77, 74, '2018-02-27']],
      dtype=object)
การจัดการข้อมูลด้วย pandas

องค์ประกอบของ DataFrame: .columns และ .index

print(dogs.columns)
Index(['name', 'breed', 'color', 'height_cm', 'weight_kg', 'date_of_birth'],
dtype='object')
dogs.index
RangeIndex(start=0, stop=7, step=1)
การจัดการข้อมูลด้วย pandas

แนวคิดของ pandas

ควรมีวิธีที่ชัดเจนเพียงหนึ่งเดียว -- และควรมีแค่วิธีเดียว -- สำหรับการทำสิ่งใดสิ่งหนึ่ง

     - The Zen of Python by Tim Peters, Item 13

มีดพับสวิสสีเขียว

1 https://www.python.org/dev/peps/pep-0020/
การจัดการข้อมูลด้วย pandas

มาฝึกกันเถอะ!

การจัดการข้อมูลด้วย pandas

Preparing Video For Download...