天天看點

Python用pandas 處理 CSV檔案 三

作者:洪較瘦不着調退役it人

回顧一下前面CSV處理的知識

文章 1 Python處理CSV檔案

文章2 Python處理CSV 之 二

Python用pandas 處理 CSV檔案 三

pandas工具作為資料分析中的利器也可以處理CSV檔案

  • 安裝pandas
pip install pandas           
  • 建立一個CSV檔案 内容如下,這是一批剛入職的員工資訊
Name,Hire Date,Salary,Sick Days remaining
Graham Chapman,03/15/14,50000.00,10
John Cleese,06/01/15,65000.00,8
Eric Idle,05/12/14,45000.00,10
Terry Jones,11/01/13,70000.00,3
Terry Gilliam,08/12/14,48000.00,7
Michael Palin,05/23/13,66000.00,8           
Python用pandas 處理 CSV檔案 三

讀取CSV檔案

import pandas
df = pandas.read_csv('hrdata.csv')
print(df)           
Name Hire Date   Salary  Sick Days remaining
0  Graham Chapman  03/15/14  50000.0                   10
1     John Cleese  06/01/15  65000.0                    8
2       Eric Idle  05/12/14  45000.0                   10
3     Terry Jones  11/01/13  70000.0                    3
4   Terry Gilliam  08/12/14  48000.0                    7
5   Michael Palin  05/23/13  66000.0                    8           
Python用pandas 處理 CSV檔案 三

可以看到每一行有一個索引,從 0 開始到行數-1

擷取所有的姓名

print(df["Name"])           
0    Graham Chapman
1       John Cleese
2         Eric Idle
3       Terry Jones
4     Terry Gilliam
5     Michael Palin
Name: Name, dtype: object

           

指定索引

import pandas
df = pandas.read_csv('hrdata.csv', index_col='Name')
print(df)
# print(df["Name"])  會報錯
print(df["Salary"])           
  • print(df) 的結果: index_col='Name' 指定了索引列此時每一行的數字變成了Name
Hire Date   Salary  Sick Days remaining
Name                                                  
Graham Chapman  03/15/14  50000.0                   10
John Cleese     06/01/15  65000.0                    8
Eric Idle       05/12/14  45000.0                   10
Terry Jones     11/01/13  70000.0                    3
Terry Gilliam   08/12/14  48000.0                    7
Michael Palin   05/23/13  66000.0                    8
           

print(df["Name"] 會報錯:

print(df["Name"])           
  • print(df["Salary"])
Python用pandas 處理 CSV檔案 三
Name
Graham Chapman    50000.0
John Cleese       65000.0
Eric Idle         45000.0
Terry Jones       70000.0
Terry Gilliam     48000.0
Michael Palin     66000.0
Name: Salary, dtype: float64           

擷取第一個人的往期時間

print(df['Hire Date'][0])           
  • df['Hire Date'] 取日期列的所有數字變成一個數組
  • df['Hire Date'][0] 取數組的第一個元素
Python用pandas 處理 CSV檔案 三
在Excelt很容易表示這個過程,選選中第二列的所有資料,然後取第一個           
Python用pandas 處理 CSV檔案 三

讀取CSV中部分列

import pandas
df = pandas.read_csv('hrdata.csv', 
            index_col='Employee', 
            parse_dates=['Hired'], 
            header=0, 
            names=['Employee', 'Hired','Salary', 'Sick Days'])
print(df)
           
Hired   Salary  Sick Days
Employee                                     
Graham Chapman 2014-03-15  50000.0         10
John Cleese    2015-06-01  65000.0          8
Eric Idle      2014-05-12  45000.0         10
Terry Jones    2013-11-01  70000.0          3
Terry Gilliam  2014-08-12  48000.0          7
Michael Palin  2013-05-23  66000.0          8           
  • index_col='Employee', Employee實際上代碼的是 Name的值
  • parse_dates=['Hired'], 處理第二列日期
  • header=0, 表示第0行是标題頭,剩下行是資料
  • names=['Employee', 'Hired','Salary', 'Sick Days']) 要顯示的列名
Python用pandas 處理 CSV檔案 三

繼續閱讀