使用 pandas 將扁平檔案匯入為 DataFrame(1)
在上一個練習中,你已經能把含有不同資料型別欄位的扁平檔案,匯入為 numpy 陣列。不過,在 pandas 中的 DataFrame 物件更適合用來儲存這類資料。所幸,我們可以輕鬆地使用 pandas 的 read_csv() 和 read_table() 函式,將混合資料型別的檔案匯入為 DataFrame。
本練習屬於課程
Python 資料匯入入門
練習說明
- 以別名
pd匯入pandas套件。 - 讀取
titanic.csv並存成名為df的 DataFrame。檔名已經存在於物件file中。 - 使用
print()呼叫來查看這個 DataFrame 的前幾列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import pandas as pd
____
# Assign the filename: file
file = 'titanic.csv'
# Read the file into a DataFrame: df
df = pd.read_csv(____)
# View the head of the DataFrame
print(____)