Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Pandas' files are read and saved


May 30, 2021 Article blog


Table of contents


Read of Excel files (read_excel)

pd.read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None, names=None, parse_cols=None, parse_dates=False, date_parser=None, na_values=None, thousands=None, convert_float=True, has_index_names=None, converters=None, dtype=None, true_values=None, false_values=None, engine=None, squeeze=False, **kwds)

Description of the parameters:

  • io: String, the path object of the file

  • Sheetname: Nonename: String, int, string list, or integer list, default 0, string for sheet name, integer for zero index sheet location, string list or integer list for requesting multiple sheets, get all sheets for None.

 Pandas' files are read and saved1

sheetname A description of the value
0 Get the sheet1 table
1 Get the sheet2 table
‘Sheet3’ Get the sheet3 table
[0,1,‘Sheet5’] Get sheet1, sheet2 and sheet5 tables
  • header: represents which row is indexed as a column, with a default of 0 (the first row), and when header is not the first row, all rows before the default column index are deleted and the data is below the column name row;

  • name: When no header is given in the file, you need to set header to None value and pass in the same number of lists as columns

    data = pd.read_excel(r'C:\w3cschool\learn\text\hello_w3cschool.xlsx',header=None,names=np.arange(19))
    print(data.head())
    
  • index_col: Specifies that it is listed as an index column, and that the default None column (0 index) is used as a row label for DataFrame. \

  • Skiprows: Skips the line before the specified row, including, to start reading

    • Any int type whose incoming value starts at 1, beyond the length of the row, is shown as empty, as follows:
      Empty DataFrame
      Columns: []
      Index: []

    • When the last piece of data is left, it appears as follows:
      Empty DataFrame
      Columns: [4, peter, 18, man]
      Index: []
      nrows

    • The value is the int type, the default Now, takes only the first n rows of data, and passes the arguments by index

    • When the incoming value is 0, take only the first line, which looks like this:
      Empty DataFrame
      Columns: [1, jack, 22, man]
      Index: []

    • When the remaining values are passed in, only the rows before that value, including, are displayed, no errors are reported beyond the length of the row, and how many rows are displayed

Read csv files (read_csv)

Note: .csv belongs to the text file, comma separates the file, the encoding is generally gbk;

Description of the parameters:

  1. sep: separator between elements, csv file default separator is comma;
  2. engine : 'c', 'python'), the underlying compilation method, default to c language, if you encounter problems, you can change the engine to Python;
  3. encoding : Generally gbk
  4. delimiter : str, default None, bounding, alternative separator (if specified, the sep parameter is invalidated)

pd.read_table( r’path’,
sep=’,’,
encoding=‘gbk’,
engine=‘python’).head()

 Pandas' files are read and saved2

Third, the preservation of documents

(1) Save to excel file: to_excel

Description of the parameters:

  • excel_writer: string or Excelwrite object, file path, or existing ExcelWriter
  • sheet_name: String, default 'Sheet1'
  • na_rep: String, default '', missing data representation

df.to_excel(‘filename.xlsx’)

(2) Save to csv file: to_csv

Description of the parameters:

  • path_or_buf : The default value is None, string or file handle, default no file, path or object, if not provided, the result will be returned as a string.
  • sep : The separator of the field of the output file, which defaults to ','
  • na_rep: Missing data means that the default is ''

, no files, paths or objects by default, if not provided, the result will be returned as a string.

  • sep : The separator of the field of the output file, which defaults to ','
  • na_rep: Missing data means that the default is ''