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

Pandas operation of Python automation series excel reading and writing

編輯:Python

pandas Kuo is python Almost the longest used library in , It has a lot of functions . Only... Is recorded here pandas Yes Excel Simple operation of files ;

pandas Introduce

Pandas yes xlwt,xlrd Encapsulation Library of Library , Have more comprehensive operation objects ,csv,excel,dataframe wait . stay xlwt On the basis of reading and writing libraries, a library can operate files of different formats . therefore pandas Dependency processing Excel Of xlrd modular ;

Simply speaking :pandas It is the encapsulation Library of the library , More powerful

pandas install

Recommended pip install :pip It's a package management tool

pip install pandas

pandas Simple introduction :

  • Import pandas

    import pandas as pd

  • pandas The most important type in DataFrame Introduction to :

    DataFrame yes Pandas An abstract data object in ( Form type ),Excel The data in can be converted into DataFrame object .

    DataFrame and Excel Properties of
    DataFrame sheet page
    Series Column
    Index Line number
    row That's ok
    NaN Empty cells


  • Simple read data
    1、 Read the file , Start with the first line , Read the first sheet
    data = pd.read_excel(‘urpan.xlsx’,header=0)

    Introduction to passing parameters when reading files :

    io: File to be read sheet_name: Specify to read the excel Which table data in the , The default is 0, This is the first table . If you pass in 1, It is the first 2 Tables ; You can specify the incoming table name , Such as "Sheet1"; You can also pass in multiple tables , Such as [0,‘Sheet3’], Pass in the first table with the name ’Sheet3’ Table of . header: Specifies the row as the column name , Default 0, That is, take the value of the first row as the column name . The data is below the column name row ; If the data does not contain column names , Is set header = None. names: The default is None, List of column names to use , If the title line is not included , Transmission... Shall be shown header=None index_col: Designate a column as , Index column usecols: Read fixed columns ,usecols=‘A:C, F’, Read A To C, and F Column :# Read the file , Start with the first line , Read the first sheetdata = pd.read_excel('H:/urpan.xlsx',header=0)print(data.head(3))print(data['year'])print(data.index) # Look at the index RangeIndex(start=0, stop=26, step=1)print(data.values) # Check the value (print(data.shape) # Check the number of lines 、 Number of columns (26, 6)print(data.head( 5 )) # See the former 5 That's ok print(data.tail( 3 )) # After viewing 3 That's ok 
  • A simple introduction to writing files

    def write():
    ‘’’’’’
    data = {‘x’:[1,2,3],‘y’:[4,5,6]}
    # convert to dataFrame
    df = pd.DataFrame(data)
    # Generate the file
    df.to_excel(‘H:/df.xlsx’,sheet_name=“df”,index=True)

    write()


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