นำเข้าข้อมูล JSON ด้วย pandas
เมื่อพัฒนาดาต้าไปป์ไลน์ คุณอาจต้องทำงานกับข้อมูลและแหล่งข้อมูลที่ไม่ใช่รูปแบบตาราง เช่น API หรือไฟล์ JSON ในแบบฝึกหัดนี้ เราจะฝึกดึงข้อมูลจากไฟล์ JSON โดยใช้ pandas
มีการนำเข้า pandas ในชื่อ pd ไว้ให้แล้ว และไฟล์ JSON ที่คุณจะนำเข้าถูกเก็บไว้ที่พาธ "testing_scores.json"
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ETL and ELT ใน Python
คำแนะนำการฝึกหัด
- ปรับปรุงฟังก์ชัน
extract()ให้อ่านไฟล์ JSON เข้าสู่ DataFrame ของpandasโดยจัดวางตาม records - ส่งพาธ
testing_scores.jsonให้กับฟังก์ชันextract()แล้วเก็บผลลัพธ์ไว้ในตัวแปรชื่อraw_testing_scores - พิมพ์ส่วนหัวของ DataFrame
raw_testing_scores
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def extract(file_path):
# Read the JSON file into a DataFrame
return pd.____(____, orient="____")
# Call the extract function with the appropriate path, assign to raw_testing_scores
____
# Output the head of the DataFrame
print(raw_testing_scores.____())