การนำเข้าไฟล์ SAS
ในแบบฝึกหัดนี้ คุณจะได้เรียนรู้วิธีนำเข้าไฟล์ SAS เป็น DataFrame โดยใช้ SAS7BDAT และ pandas ไฟล์ 'sales.sas7bdat' อยู่ใน working directory ของคุณแล้ว และได้นำเข้า pandas กับ matplotlib.pyplot ไว้ดังนี้:
import pandas as pd
import matplotlib.pyplot as plt
ข้อมูลดัดแปลงมาจากเว็บไซต์ของตำราระดับปริญญาตรี Principles of Econometrics โดย Hill, Griffiths และ Lim
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Python เบื้องต้น: การนำเข้าข้อมูล
คำแนะนำการฝึกหัด
- นำเข้าโมดูล
SAS7BDATจากไลบรารีsas7bdat - ในบริบทของไฟล์
'sales.sas7bdat'ให้โหลดข้อมูลลงใน DataFrame ชื่อdf_sasโดยเรียกใช้เมธอด.to_data_frame()บนออบเจกต์file - แสดง head ของ DataFrame
df_sas - รันสคริปต์ทั้งหมดเพื่อสร้างกราฟฮิสโตแกรม!
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import sas7bdat package
from ____ import ____
# Save file to a DataFrame: df_sas
with SAS7BDAT('sales.sas7bdat') as file:
____
# Print head of DataFrame
# Plot histogram of DataFrame features (pandas and pyplot already imported)
pd.DataFrame.hist(df_sas[['P']])
plt.ylabel('count')
plt.show()