เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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

ผลลัพธ์ของดาต้าไปป์ไลน์โดยทั่วไปคือชุดข้อมูลที่ผ่านการ "โมเดล" (modeled) แล้ว ชุดข้อมูลนี้ช่วยให้ผู้ใช้ข้อมูลเข้าถึงข้อมูลได้ง่าย โดยไม่ต้องจัดการข้อมูลมากนัก การจัดกลุ่มข้อมูลด้วย pandas ช่วยสร้างชุดข้อมูลที่ผ่านการโมเดลเหล่านี้ได้

pandas ถูกนำเข้ามาในชื่อ pd แล้ว และ DataFrame ชื่อ raw_testing_scores มีข้อมูลในรูปแบบดังนี้:

              street_address       city  math_score  reading_score  writing_score
01M539   111 Columbia Street  Manhattan       657.0          601.0          601.0
02M294      350 Grand Street  Manhattan       395.0          411.0          387.0
02M308      350 Grand Street  Manhattan       418.0          428.0          415.0

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

ETL and ELT ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ใช้ .loc[] เพื่อเก็บเฉพาะคอลัมน์ "city", "math_score", "reading_score" และ "writing_score"
  • จัดกลุ่ม DataFrame ตามคอลัมน์ "city" แล้วหาค่าเฉลี่ยของคะแนนคณิตศาสตร์ การอ่าน และการเขียนของแต่ละเมือง
  • ใช้ฟังก์ชัน transform() เพื่อสร้าง DataFrame ที่ผ่านการจัดกลุ่ม

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

def transform(raw_data):
	# Use .loc[] to only return the needed columns
	raw_data = raw_data.____[:, ____]
	
    # Group the data by city, return the grouped DataFrame
	grouped_data = raw_data.____(by=["____"], axis=0).____()
	return grouped_data

# Transform the data, print the head of the DataFrame
grouped_testing_scores = ____(raw_testing_scores)
print(grouped_testing_scores.head())
แก้ไขและรันโค้ด