Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
一、三種數(shù)據(jù)文件的讀取

二、csv、tsv、txt 文件讀取
1)CSV文件讀取:
語法格式:pandas.read_csv(文件路徑)
CSV文件內(nèi)容如下:

import pandas as pd file_path = "e:\\pandas_study\\test.csv" content = pd.read_csv(file_path) content.head() # 默認返回前5行數(shù)據(jù) content.head(3) # 返回前3行數(shù)據(jù) content.shape # 返回一個元組(總行數(shù),總列數(shù)),總行數(shù)不包括標題行 content.index # 返回索引,是一個可迭代的對象<class 'pandas.core.indexes.range.RangeIndex'> content.column # 返回所有的列名 Index(['姓名', '年齡', '籍貫'], dtype='object') content.dtypes # 返回的是每列的數(shù)據(jù)類型 姓名 object 年齡 int64 籍貫 object dtype: object
2)CSV文件讀?。?/strong>
語法格式:pandas.read_csv(文件路徑)
CSV文件內(nèi)容如下:

import pandas as pd file_path = "e:\\pandas_study\\test2.txt" content = pd.read_csv(file_path,sep='\t',header = None ,names= ['name','age','adress']) #參數(shù)說明: # header = None 表示沒有標題行 # sep='\t' 表示去除分割符中的空格 # names= ['name','age','adress'] ,列名依次自定義為'name','age','adress' content.head() # 默認返回前5行數(shù)據(jù) content.head(3) # 返回前3行數(shù)據(jù) content.shape # 返回一個元組(總行數(shù),總列數(shù)),總行數(shù)不包括標題行 content.index # 返回索引,是一個可迭代的對象<class 'pandas.core.indexes.range.RangeIndex'> content.column # 返回所有的列名 Index(['姓名', '年齡', '籍貫'], dtype='object') content.dtypes # 返回的是每列的數(shù)據(jù)類型
三、excel文件讀取

import pandas as pd file_path = "e:\\pandas_study\\test3.xlsx" content = pd.read_excel(file_path) content.head() # 默認返回前5行數(shù)據(jù) content.head(3) # 返回前3行數(shù)據(jù) content.shape # 返回一個元組(總行數(shù),總列數(shù)),總行數(shù)不包括標題行 content.index # 返回索引,是一個可迭代的對象<class 'pandas.core.indexes.range.RangeIndex'> content.column # 返回所有的列名 Index(['姓名', '年齡', '籍貫'], dtype='object') content.dtypes # 返回的是每列的數(shù)據(jù)類型 姓名 object 年齡 int64 籍貫 object dtype: object
四、數(shù)據(jù)庫表格讀取
語法: pandas.read_sql(sql語句,數(shù)據(jù)庫連接對象)
數(shù)據(jù)對象的創(chuàng)建,可以根據(jù)pymysql,cx_oracle等模塊連接mysql或者oracle。
到此這篇關(guān)于Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)的文章就介紹到這了,更多相關(guān)pandas讀取數(shù)據(jù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信