Python用pandas讀取excel資料

2021-10-09 15:15:05 字數 1452 閱讀 1522

#python匯入資料主要用的是read_x()方法,x表示匯入檔案的格式

#匯入.xlsx檔案,用read_excel()

print("-"*10+"用python如何讀取資料源和熟悉資料"+"-"*10)

print("嗶哩嗶哩python課程,主要講用pandas獲取資料源:")

print("1.匯入資料\n2.熟悉資料")

print('\n')

print("-"*15+"1.匯入資料"+"-"*15)

import pandas as pd

"""df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx")#讀excel**:r」檔案路徑「

#快速得到檔案路徑的方法:將檔案拖到cmd終端

print("excel的資料是","*"*15)

print(df,'\n')#xlsx檔案的第一行被作為列索引(或者做表頭),不計入下標索引,從第二行開始索引為0

#print(df[4:10])#讀取xlsx檔案的下標索引從4開始時的行 【4,10)

""""""

#指定哪個sheet,可以根據sheet的名字訪問,也可以根據索引訪問:sheet_name=

df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx",sheet_name="sheet2")

print("excel的sheet1資料是","*"*15)

print(df[:])

df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx",sheet_name=2)#下標索引從0開始

print("excel的sheet1資料是","*"*15)

print(df[:])

""""""

#指定哪一列作為列索引:header=

df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx",header=3)#指定index==3的行(即第4行)作為列索引

print(df[0:10])#此時將指定列索引下面的行作為索引0的行,不再是從excel的真正的第一行開始列印資料

""""""

#也可以指定行索引

df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx",index_col=2)#指定index=2的列作為行索引

print(df[0:10])#指定做行索引的一列資料到了整個表的第一列,其他不變

""""""

#匯入指定的列

df=pd.read_excel(r"c:/users/rita/desktop/none920.xlsx",usecols=[2,6])#匯入索引為2,6的兩列資料

print(df)

"""

Python使用pandas讀取csv和excel

詳細引數見 得到data資料框,index為time,變數名為value,index col 0表示將data中的第乙個變數設定成index data pd.read csv data.csv header none,names time value index col 0 得到data資料框,sh...

python中pandas讀取txt檔案注意事項

語法 pandas.read table 引數 filepath or buffer 檔案路徑或者輸入物件 sep 分隔符,預設為製表符 names 讀取哪些列以及讀取列的順序,預設按順序讀取所有列 engine 檔案路徑包含中文的時候,需要設定engine python encoding 檔案編碼...

python的pandas庫讀取csv

首先建立test.csv原始資料,內容如下 時間,地點 一月,北京 二月,上海 三月,廣東 四月,深圳 五月,河南 六月,鄭州 七月,新密 八月,大連 九月,盤錦 十月,瀋陽 十一月,武漢 十二月,南京 匯出pandas import pandas as pd csv pd.read csv tes...