การนำเข้าข้อมูล JSON ด้วย pandas
ในการพัฒนา data pipeline บางครั้งอาจต้องทำงานกับข้อมูลและแหล่งข้อมูลที่ไม่ใช่ตาราง เช่น API หรือไฟล์ JSON ในแบบฝึกหัดนี้ จะฝึกดึงข้อมูลจากไฟล์ JSON โดยใช้ pandas
pandas ถูก import แล้วในชื่อ pd และไฟล์ JSON ที่จะนำเข้าอยู่ที่ path "testing_scores.json"
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ETL และ ELT ด้วย Python
คำแนะนำการฝึกหัด
- อัปเดตฟังก์ชัน
extract()ให้อ่านไฟล์ JSON เข้าสู่pandasDataFrame โดยกำหนด orientation เป็น records - ส่ง path
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.____())