การเลือกคอลัมน์

Python เบื้องต้นสำหรับ Data Science

Hillary Green-Lerman

Lead Data Scientist, Looker

ทำไมต้องเลือกคอลัมน์?

  • ใช้ในการคำนวณ

    credit_records.price.sum()
    
  • พล็อตข้อมูล

    plt.plot(ransom['letter'], ransom['frequency'])
    
Python เบื้องต้นสำหรับ Data Science

ชื่อคอลัมน์เป็น string

print(credit_records.head())
            suspect         location              date         item  price
0    Kirstine Smith   Groceries R Us   January 6, 2018     broccoli   1.25
1      Gertrude Cox  Petroleum Plaza   January 6, 2018  fizzy drink   1.90
2  Fred Frequentist   Groceries R Us   January 6, 2018     broccoli   1.25
3      Gertrude Cox   Groceries R Us  January 12, 2018     broccoli   1.25
4    Kirstine Smith    Clothing Club   January 9, 2018        shirt  14.25
'suspect'
'location'
'date'
'item'
'price'
Python เบื้องต้นสำหรับ Data Science

การเลือกด้วยวงเล็บก้ามปูและ string

suspect = credit_records['suspect']
print(suspect)
0            Kirstine Smith
1              Gertrude Cox
2          Fred Frequentist
3              Gertrude Cox
4            Kirstine Smith
5              Gertrude Cox
...
99             Gertrude Cox
100        Fred Frequentist
101            Gertrude Cox
102          Kirstine Smith
103    Ronald Aylmer Fisher
Python เบื้องต้นสำหรับ Data Science

การเลือกด้วยจุด

price = credit_records.price
print(price)
0       1.25
1       1.90
2       1.25
3       1.25
4      14.25
5       3.95
...
99     14.25
100    12.05
101    20.15
102     3.95
103     2.05
Python เบื้องต้นสำหรับ Data Science

ข้อผิดพลาดที่พบบ่อยในการเลือกคอลัมน์

ใช้วงเล็บก้ามปูและ string สำหรับชื่อคอลัมน์ที่มีช่องว่างหรืออักขระพิเศษ (-, ? เป็นต้น)

police_report['Is Golden Retriever?']

NOT

police_report.Is Golden Retriever?
Object `Retriever` not found.
Python เบื้องต้นสำหรับ Data Science

ข้อผิดพลาดที่พบบ่อยในการเลือกคอลัมน์

เมื่อใช้วงเล็บก้ามปูและ string อย่าลืมใส่เครื่องหมายคำพูดรอบชื่อคอลัมน์!

credit_report['location']

NOT

credit_report[location]
Object `location` not found.
Python เบื้องต้นสำหรับ Data Science

ข้อผิดพลาดที่พบบ่อยในการเลือกคอลัมน์

ใช้วงเล็บก้ามปู ไม่ใช่วงเล็บกลม

credit_report['location']

NOT

credit_report('location')
----------------------------------------------------------------------
TypeError  Traceback (most recent call last)
<ipython-input-5-aabdb8981438> in <module>()
----> 1 credit_report('location')

TypeError: 'DataFrame' object is not callable
Python เบื้องต้นสำหรับ Data Science

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

Python เบื้องต้นสำหรับ Data Science

Preparing Video For Download...