Python操作CSV格式文件的方法大全
(一)CSV格式文件
1.說明
CSV是一種以逗號分隔數(shù)值的文件類型,在數(shù)據(jù)庫或電子表格中,常見的導入導出文件格式就是CSV格式,CSV格式存儲數(shù)據(jù)通常以純文本的方式存數(shù)數(shù)據(jù)表。
(二)CSV庫操作csv格式文本
操作一下表格數(shù)據(jù):
1.讀取表頭的2中方式
#方式一 import csv with open("D:\\test.csv") as f: reader = csv.reader(f) rows=[row for row in reader] print(rows[0]) ---------- #方式二 import csv with open("D:\\test.csv") as f: #1.創(chuàng)建閱讀器對象 reader = csv.reader(f) #2.讀取文件第一行數(shù)據(jù) head_row=next(reader) print(head_row)
結果演示:['姓名', '年齡', '職業(yè)', '家庭地址', '工資']
2.讀取文件某一列數(shù)據(jù)
#1.獲取文件某一列數(shù)據(jù) import csv with open("D:\\test.csv") as f: reader = csv.reader(f) column=[row[0] for row in reader] print(column)
結果演示:['姓名', '張三', '李四', '王五', 'Kaina']
3.向csv文件中寫入數(shù)據(jù)
#1.向csv文件中寫入數(shù)據(jù) import csv with open("D:\\test.csv",'a') as f: row=['曹操','23','學生','黑龍江','5000'] write=csv.writer(f) write.writerow(row) print("寫入完畢!")
結果演示:
4.獲取文件頭及其索引
import csv with open("D:\\test.csv") as f: #1.創(chuàng)建閱讀器對象 reader = csv.reader(f) #2.讀取文件第一行數(shù)據(jù) head_row=next(reader) print(head_row) #4.獲取文件頭及其索引 for index,column_header in enumerate(head_row): print(index,column_header)
結果演示:
['姓名', '年齡', '職業(yè)', '家庭地址', '工資']
0 姓名
1 年齡
2 職業(yè)
3 家庭地址
4 工資
5.獲取某列的最大值
# ['姓名', '年齡', '職業(yè)', '家庭地址', '工資'] import csv with open("D:\\test.csv") as f: reader = csv.reader(f) header_row=next(reader) # print(header_row) salary=[] for row in reader: #把第五列數(shù)據(jù)保存到列表salary中 salary.append(int(row[4])) print(salary) print("員工最高工資為:"+str(max(salary)))
結果演示:員工最高工資為:10000
6.復制CSV格式文件
原文件test.csv
import csv f=open('test.csv') #1.newline=''消除空格行 aim_file=open('Aim.csv','w',newline='') write=csv.writer(aim_file) reader=csv.reader(f) rows=[row for row in reader] #2.遍歷rows列表 for row in rows: #3.把每一行寫到Aim.csv中 write.writerow(row)
01.未添加關鍵字參數(shù)newline=' '的結果:
02添加關鍵字參數(shù)newline=' '的Aim.csv文件的內容:
(三)pandas庫操作CSV文件
csv文件內容:
1.安裝pandas庫:pip install pandas
2.讀取csv文件所有數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) print(data)
結果演示:
姓名 年齡 職業(yè) 家庭地址 工資
0 張三 22 廚師 北京市 6000
1 李四 26 攝影師 湖南長沙 8000
2 王五 28 程序員 深圳 10000
3 Kaina 22 學生 黑龍江 2000
4 曹操 28 銷售 上海 6000
3.describe()方法數(shù)據(jù)統(tǒng)計
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #了解更多describe()知識,ctr+鼠標左鍵 print(data.describe())
結果演示:
年齡 工資
count 5.00000 5.000000
mean 25.20000 6400.000000
std 3.03315 2966.479395
min 22.00000 2000.000000
25% 22.00000 6000.000000
50% 26.00000 6000.000000
75% 28.00000 8000.000000
max 28.00000 10000.000000
4.讀取文件前幾行數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #讀取前2行數(shù)據(jù) # head_datas = data.head(0) head_datas=data.head(2) print(head_datas)
結果演示:
姓名 年齡 職業(yè) 家庭地址 工資
0 張三 22 廚師 北京市 6000
1 李四 26 攝影師 湖南長沙 8000
5.讀取某一行所有數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #讀取第一行所有數(shù)據(jù) print(data.ix[0,])
結果演示:
姓名 張三
年齡 22
職業(yè) 廚師
家庭地址 北京市
工資 6000
6.讀取某幾行的數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #讀取第一行、第二行、第四行的所有數(shù)據(jù) print(data.ix[[0,1,3],:])
結果演示:
姓名 年齡 職業(yè) 家庭地址 工資
0 張三 22 廚師 北京市 6000
1 李四 26 攝影師 湖南長沙 8000
3 Kaina 22 學生 黑龍江 2000
7.讀取所有行和列數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #讀取所有行和列數(shù)據(jù) print(data.ix[:,:])
結果演示:
姓名 年齡 職業(yè) 家庭地址 工資
0 張三 22 廚師 北京市 6000
1 李四 26 攝影師 湖南長沙 8000
2 王五 28 程序員 深圳 10000
3 Kaina 22 學生 黑龍江 2000
4 曹操 28 銷售 上海 6000
8.讀取某一列的所有行數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) # print(data.ix[:, 4]) print(data.ix[:,'工資'])
結果演示:
0 6000
1 8000
2 10000
3 2000
4 6000
Name: 工資, dtype: int64
9.讀取某幾列的某幾行
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) print(data.ix[[0,1,3],['姓名','職業(yè)','工資']])
結果演示:
姓名 職業(yè) 工資
0 張三 廚師 6000
1 李四 攝影師 8000
3 Kaina 學生 2000
10.讀取某一行和某一列對應的數(shù)據(jù)
import pandas as pd path= 'D:\\test.csv' with open(path)as file: data=pd.read_csv(file) #讀取第三行的第三列 print("職業(yè)---"+data.ix[2,2])
結果演示:職業(yè)---程序員
11.CSV數(shù)據(jù)的導入導出(復制CSV文件)
讀方式01:
import pandas as pd #1.讀入數(shù)據(jù) data=pd.read_csv(file)
寫出數(shù)據(jù)02:
import pandas as pd #1.寫出數(shù)據(jù),目標文件是Aim.csv data.to_csv('Aim.csv')
其他:
01.讀取網絡數(shù)據(jù): import pandas as pd data_url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv" #填寫url讀取 df = pd.read_csv(data_url) ---------- 02.讀取excel文件數(shù)據(jù) import pandas as pd data = pd.read_excel(filepath)
實例演示:
1.test.csv原文件內容
2.現(xiàn)在把test.csv中的內容復制到Aim.csv中
import pandas as pd file=open('test.csv') #1.讀取file中的數(shù)據(jù) data=pd.read_csv(file) #2.把data寫到目標文件Aim.csv中 data.to_csv('Aim.csv') print(data)
結果演示:
注:pandas模塊處理Excel文件和處理CSV文件差不多!
參考文檔:https://docs.python.org/3.6/library/csv.html
總結
到此這篇關于Python操作CSV格式文件的文章就介紹到這了,更多相關Python操作CSV文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談Series和DataFrame中的sort_index方法
今天小編就為大家分享一篇淺談Series和DataFrame中的sort_index方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06