程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python get excel data

編輯:Python

The excel to be imported is:

You can use the read_excel function of pandas, the specific code is as follows:

import pandas as pdgetdata=pd.read_excel(r'C:/folder index/filename.xlsx',sheet_name='name of worksheet sheet')

If sheet_name does not set parameters, it will default to the first worksheet. At the same time, the position of the worksheet can also be set. When reading the fifth worksheet, it can be set to =4.

One, column operation

If you want to get one or more columns in the worksheet, you can use the usecols parameter. For example, to read the 2nd to 5th columns of the 5th worksheet, you can use the following code:

import pandas as pdgetdata=pd.read_excel(r'C:/folder index/filename.xlsx',sheet_name='name of worksheet sheet',sheet_name=4,usecols=[i for i in range (1,6)])

The usecols parameter can also be set to the index letter of the column, such as usecols="B, D:E", you can get the 1st and 3rd to 5th columns, and set the parameter index_col=1 at the same time, take the second column as the index,The code and output result are:

getdata=pd.read_excel(r'C:/folder index/filename.xlsx',sheet_name='name of worksheet sheet',sheet_name=4,usecols="A,C:E",index_col=1)print(Getdata)

Second, line operation

If you don't want to get all the rows, you can set the parameter nrows=5 to get the first 5 rows, and skip the 2nd to 4th rows at the same time, you can set the parameter skiprows=[i for i in range(2,5)], or skiprows=[2,3,4], code and output:

getdata=pd.read_excel(r'C:/folder index/filename.xlsx',sheet_name='name of worksheet sheet',skiprows=[2,3,4],nrows=5)print(Getdata)

It should be noted here that the set nrows is the total number of rows to be acquired. If skiprows is set to skip a certain number of rows, it will continue to be acquired in the following rows until the number of rows to be acquired by nrows is made up.

The first article I wrote may not be exhaustive and will be supplemented later


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved