Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
1)CSV文件讀取:
語(yǔ)法格式:pandas.read_csv(文件路徑)CSV文件內(nèi)容如下:
import pandas as pdfile_path = 'e:pandas_studytest.csv'content = pd.read_csv(file_path)content.head() # 默認(rèn)返回前5行數(shù)據(jù)content.head(3) # 返回前3行數(shù)據(jù)content.shape # 返回一個(gè)元組(總行數(shù),總列數(shù)),總行數(shù)不包括標(biāo)題行content.index # 返回索引,是一個(gè)可迭代的對(duì)象<class ’pandas.core.indexes.range.RangeIndex’>content.column # 返回所有的列名 Index([’姓名’, ’年齡’, ’籍貫’], dtype=’object’)content.dtypes # 返回的是每列的數(shù)據(jù)類型姓名 object年齡 int64籍貫 objectdtype: object
2)CSV文件讀取:
語(yǔ)法格式:pandas.read_csv(文件路徑)CSV文件內(nèi)容如下:
import pandas as pdfile_path = 'e:pandas_studytest2.txt'content = pd.read_csv(file_path,sep=’t’,header = None ,names= [’name’,’age’,’adress’])#參數(shù)說(shuō)明:# header = None 表示沒(méi)有標(biāo)題行# sep=’t’ 表示去除分割符中的空格# names= [’name’,’age’,’adress’] ,列名依次自定義為’name’,’age’,’adress’content.head() # 默認(rèn)返回前5行數(shù)據(jù)content.head(3) # 返回前3行數(shù)據(jù)content.shape # 返回一個(gè)元組(總行數(shù),總列數(shù)),總行數(shù)不包括標(biāo)題行content.index # 返回索引,是一個(gè)可迭代的對(duì)象<class ’pandas.core.indexes.range.RangeIndex’>content.column # 返回所有的列名 Index([’姓名’, ’年齡’, ’籍貫’], dtype=’object’)content.dtypes # 返回的是每列的數(shù)據(jù)類型三、excel文件讀取
import pandas as pdfile_path = 'e:pandas_studytest3.xlsx'content = pd.read_excel(file_path)content.head() # 默認(rèn)返回前5行數(shù)據(jù)content.head(3) # 返回前3行數(shù)據(jù)content.shape # 返回一個(gè)元組(總行數(shù),總列數(shù)),總行數(shù)不包括標(biāo)題行content.index # 返回索引,是一個(gè)可迭代的對(duì)象<class ’pandas.core.indexes.range.RangeIndex’>content.column # 返回所有的列名 Index([’姓名’, ’年齡’, ’籍貫’], dtype=’object’)content.dtypes # 返回的是每列的數(shù)據(jù)類型姓名 object年齡 int64籍貫 objectdtype: object四、數(shù)據(jù)庫(kù)表格讀取
語(yǔ)法: pandas.read_sql(sql語(yǔ)句,數(shù)據(jù)庫(kù)連接對(duì)象)數(shù)據(jù)對(duì)象的創(chuàng)建,可以根據(jù)pymysql,cx_oracle等模塊連接mysql或者oracle。
到此這篇關(guān)于Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)的文章就介紹到這了,更多相關(guān)pandas讀取數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. css代碼優(yōu)化的12個(gè)技巧2. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單3. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法4. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法8. HTML DOM setInterval和clearInterval方法案例詳解9. XHTML 1.0:標(biāo)記新的開(kāi)端10. asp批量添加修改刪除操作示例代碼
