Life is short. I use Python

Previous portal:

Data Analysis (1) : Fundamentals of data analysis

(2) The text is used to analyze data in Python

(2) The Series of data structures in Python is written for Pandas

(3) The Python data structure is used in Pandas

(1) Viewing data for Pandas (2) Viewing data for Pandas

(5) Basic operations (2) Data selection

(6) Data import for Pandas

(7) Data processing for Pandas

(2) The text is written in Python. (3) The text is written in Python.

(9) The computer is used to manipulate data

(10) Grouping data in Python

(12) Pandas (12) The Library is equipped with a pivot_table.

(12) Concatenation of tables in Python

The introduction

First, as an aside, a child left a message to me in the background today, saying that my profile picture on the official account was too scary and I dared not follow me, for fear that I would invade her mobile phone…

Emmmmmmmmmmmm, I am so cow force, I don’t know how.

Don’t know how many people is because my head is too scary can’t focus on me, so I decided to ask the great wisdom of the fan group for a face, do not have what request, positive, sunshine, positive, can, if I can, and the name of the public, a geek or excavators, link up with the best way to add small make up WeChat (allen_6174) issued to small make up, If it is adopted, it will be replaced across all platforms (poor me, I’ve been using my avatar for nearly a year, I thought it was cool).

So without further ado, let’s get to the topic of the day, data export.

When we want to save the data after processing the data, we need to use the data export.

In fact, in the previous article, xiaobian once used to export data into Excel, did not introduce in detail at that time, this article will introduce data export in detail.

Excel export

In order to export the data, we need to have a copy of the DataFrame data. In this example, we need to read the Excel data directly.

The to_excel() method is used when exporting an Excel file to Pandas. The to_excel() method is used when exporting an Excel file to Pandas.

import pandas as pd

# data read
df = pd.read_excel("table_join_exp.xlsx", sheet_name='Sheet1')

# Data export
df.to_excel(excel_writer=r'D:\Development\Projects\demo.xlsx')
Copy the code

Let’s look at the syntax of the to_excel() method:

DataFrame.to_excel(self, excel_writer, sheet_name='Sheet1', na_rep=' ', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None) -None
Copy the code

To_excel () = to_excel() = to_excel()

  • Excel_writer: indicates the file path or an existing ExcelWriter
  • Sheet_name: string, default is “Sheet1”
  • Float_format: Indicates the string used to format floating point numbers
  • Header: Writes the column name. If a list of strings is given, it is assumed to be an alias for the column name.
  • Index: writes an index
  • Columns: Sets the columns to be exported
  • Encoding: Specifies the encoding format'utf-8'
  • Na_rep: missing value processing
  • Inf_rep: Infinite processing

Next, let’s look at the example. This example will be as complex as possible, using all of the above attributes as possible, and annotating them:

df.to_excel(excel_writer=r'D:\Development\Projects\demo.xlsx',
            sheet_name='Test document'.Create sheet name
            index=False.# set index not to display
            columns=['number'.'name'].Set the columns to export
            encoding='utf-8'.Set the encoding format
            na_rep='0'.# Missing value handling
            inf_rep='inf'  # infinite processing
            )
Copy the code

CSV export

To_csv () is used to export CSV files, which is not much different from Excel. You need to set the file path first, then you can set the index, exported columns, delimiters, encoding format, missing values, and so on.

Let’s start with the to_csv() syntax:

DataFrame.to_csv(self, path_or_buf: Union[str, pathlib.Path, IO[~AnyStr], NoneType] = None, sep: str = ', ', na_rep: str = ' ', float_format: Union[str, NoneType] = None, columns: Union[Sequence[Union[Hashable, NoneType]], NoneType] = None, header: Union[bool, List[str]] = True, index: bool = True, index_label: Union[bool, str, Sequence[Union[Hashable, NoneType]], NoneType] = None, mode: str = 'w', encoding: Union[str, NoneType] = None, compression: Union[str, Mapping[str, str], NoneType] = 'infer', quoting: Union[int, NoneType] = None, quotechar: str = '"', line_terminator: Union[str, NoneType] = None, chunksize: Union[int, NoneType] = None, date_format: Union[str, NoneType] = None, doublequote: bool = True, escapechar: Union[str, NoneType] = None, decimal: Union[str, NoneType] = '. ') - the Union [STR, NoneType]Copy the code

To_csv () has more parameters than to_excel().

df.to_csv(path_or_buf=r'D:\Development\Projects\demo.csv'.Set the export path
          index=False.# set index not to display
          sep=', '.# set the delimiter
          na_rep='0'.# Missing value handling
          columns=['number'.'name'].Set the columns to export
          encoding='utf-8'.Set the encoding format
          )
Copy the code

The notes have been written very clearly, I will not do more introduction.

This is the end of the Pandas series. In the next part of the series, we will introduce the use of Matplotlib for data visualization

The sample code

As usual, all sample code will be uploaded to Github and Gitee for easy access.

Example code -Github

Example code -Gitee