扁平檔在資料科學中的重要性

Python 資料匯入入門

Hugo Bowne-Anderson

Data Scientist at DataCamp

扁平檔(Flat files)

titanic.csv

PassengerId,Survived,Pclass,Name,Gender,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S
2,1,1,"Cumings, Mrs. John Bradley",female,38,1,0,PC 17599,71.2833,C85,C
3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2.3101282,7.925,,S
Python 資料匯入入門

扁平檔(Flat files)

titanic.csv

PassengerId,Survived,Pclass,Name,Gender,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked

1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S

2,1,1,"Cumings, Mrs. John Bradley",female,38,1,0,PC 17599,71.2833,C85,C

3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2.3101282,7.925,,S

arrow

                        Name      Sex  Cabin  Survived
     Braund, Mr. Owen Harris     male    NaN         0
  Cumings, Mrs. John Bradley   female    C85         1
      Heikkinen, Miss. Laina   female    NaN         1
Futrelle, Mrs. Jacques Heath   female   C123         1
    Allen, Mr. William Henry     male    NaN         0
Python 資料匯入入門

扁平檔(Flat files)

titanic.csv

titanic.csv with a row highlighted arrow

                        Name      Sex  Cabin  Survived
     Braund, Mr. Owen Harris     male    NaN         0
  Cumings, Mrs. John Bradley   female    C85         1
      Heikkinen, Miss. Laina   female    NaN         1
Python 資料匯入入門

扁平檔(Flat files)

titanic.csv

titanic.csv with a column highlighted arrow

                        Name      Sex  Cabin  Survived
     Braund, Mr. Owen Harris     male    NaN         0
  Cumings, Mrs. John Bradley   female    C85         1
      Heikkinen, Miss. Laina   female    NaN         1
Python 資料匯入入門

扁平檔(Flat files)

  • 以文字檔儲存紀錄
  • 也就是表格資料
  • 紀錄:由欄位或屬性組成的一列

 

titanic.csv

PassengerId,Survived,Pclass,Name,Gender,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S
2,1,1,"Cumings, Mrs. John Bradley",female,38,1,0,PC 17599,71.2833,C85,C
3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2.3101282,7.925,,S
Python 資料匯入入門

扁平檔(Flat files)

  • 以文字檔儲存紀錄
  • 也就是表格資料
  • 紀錄:由欄位或屬性組成的一列
  • 欄:特徵或屬性

titanic.csv

titanic.csv with a row highlighted arrow

Python 資料匯入入門

扁平檔(Flat files)

  • 以文字檔儲存紀錄
  • 也就是表格資料
  • 紀錄:由欄位或屬性組成的一列
  • 欄:特徵或屬性

titanic.csv

titanic.csv with a column highlighted arrow

Python 資料匯入入門

欄位名稱列(Header)

titanic.csv

________________________________________________________________________
PassengerId,Survived,Pclass,Name,Gender,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
________________________________________________________________________
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S   
2,1,1,"Cumings, Mrs. John Bradley",female,38,1,0,PC 17599,71.2833,C85,C
3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2.3101282,7.925,,S
Python 資料匯入入門

欄位名稱列(Header)

titanic.csv

________________________________________________________________________
PassengerId,Survived,Pclass,Name,Gender,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
________________________________________________________________________
1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S   
2,1,1,"Cumings, Mrs. John Bradley",female,38,1,0,PC 17599,71.2833,C85,C
3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2.3101282,7.925,,S
Python 資料匯入入門

副檔名(File extension)

  • .csv:逗號分隔值
  • .txt:文字檔
  • 逗號、tab:分隔符號
Python 資料匯入入門

Tab 分隔檔

MNIST.txt

pixel149    pixel150    pixel151    pixel152    pixel153
0           0           0           0           0    
86          250         254         254         254    
0           0           0           9           254    
0           0           0           0           0    
103         253         253         253         253    
0           0           0           0           0    
0           0           0           0           0        
0           0           0           0           41        
253         253         253         253         253    
Python 資料匯入入門

Tab 分隔檔

MNIST.txt

pixel149    pixel150    pixel151    pixel152    pixel153
0           0           0           0           0    
86          250         254         254         254    
0           0           0           9           254    
0           0           0           0           0    
103         253         253         253         253    
0           0           0           0           0    
0           0           0           0           0        
0           0           0           0           41        
253         253         253         253         253    

                                         MNIST 影像: mnist.png

Python 資料匯入入門

如何匯入扁平檔?

  • 兩大套件:NumPy、pandas

ch_1_2.026.png

  • 這裡你會學到如何匯入:
    • 數值型扁平檔(MNIST)
    • 數值與字串混合的扁平檔(titanic.csv)
Python 資料匯入入門

一起來練習吧!

Python 資料匯入入門

Preparing Video For Download...