[Python] How to name table data and output it in csv (to_csv method)

[Python] How to name table data and output it in csv (to_csv method)

How to output the table data read by the pandas module and the processed / created table data locally as a csv file.

Basic syntax (basic option)

to_csv ('file path', encording ='utf_8_sig', index = False) └ ① "'File path'": Absolute path or relative path is acceptable └ 2 "encoding ='utf_8_sig'": Character code specification └ ③ "index = False": An instruction that does not include the index number column that is automatically generated when a table is read or created with pandas.

① is required. ② and ③ can be omitted.


** ▼ Supplement About "utf_8_sig" ** Something like upward compatibility with utf_8. Both utf8 with and without BOM can be read normally.

■ Example

** ▼ Example: When outputting the following table **

The variable df contains the table below.

image.png

How to read csv file with python here

▼ Output as csv file

Illustration


df.to_csv("~/desktop/output.csv",index = False,encoding = "utf_8_sig")

** ▼ Details ** ①「df.to_csv」 └ Convert data df (variable) to csv file. ②「"~/desktop/output.csv"」 └ Specify the output destination with an absolute path. └ Output to the desktop with the file name "output.csv". └ Overwrite if the same file exists.

③「index = False」 └ No index number in the first column is required

④「encoding = "utf_8_sig"」 └ Character code has utf8 signature.


### ■ Output result ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563526/ae6a78d6-a08a-bde9-228f-e12a054e4cab.png)
## ■ Garbled characters and errors ### ▼ Characters are garbled if no character code is specified.

python


df.to_csv("~/desktop/output.csv",index = False)

image.png

▼ Error if the output file name is open

error


df.to_csv("~/desktop/output.csv",index = False)

#output
# PermissionError: [Errno 13] Permission denied: 'C:\\Users\desktop/output.csv'

"Permission denied:" It looks like it was strongly denied, but it just couldn't be overwritten.

Recommended Posts

[Python] How to name table data and output it in csv (to_csv method)
processing to use notMNIST data in Python (and tried to classify it)
Data input / output in Python (CSV, JSON)
How to install OpenCV on Cloud9 and run it in Python
[Python] How to delete rows and columns in a table (list of drop method options)
How to display multiplication table in python
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to use is and == in Python
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
How to make a container name a subdomain and make it accessible in Docker
How to generate permutations in Python and C ++
[Python] How to read data from CIFAR-10 and CIFAR-100
How to output "Ketsumaimo" as standard output in Python
How to plot autocorrelation and partial autocorrelation in python
How to input a character string in Python and output it as it is or in the opposite direction.
How to get the variable name itself in python
Full-width and half-width processing of CSV data in Python
[Python] How to sort dict in list and instance in list
How to save a table scraped by python to csv
How to use Decorator in Django and how to make it
[Python] How to output the list values in order
How to read csv containing only integers in Python
[GCF + Python] How to upload Excel to GCS and create a new table in BigQuery
How to develop in Python
It is easy to execute SQL with Python and output the result in Excel
Try to make it using GUI and PyQt in Python
How to swap elements in an array in Python, and how to reverse an array.
Foreigners talk: How to name classes and methods in English
How to use the __call__ method in a Python class
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to generate exponential pulse time series data in python
I tried "How to get a method decorated in Python"
Comparison of how to use higher-order functions in Python 2 and 3
How to convert Youtube to mp3 and download it super-safely [Python]
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Python] How to output a pandas table to an excel file
How to execute external shell scripts and commands in python
Data science companion in python, how to specify elements in pandas
How to log in to AtCoder with Python and submit automatically
Overview of Python virtual environment and how to create it
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
Output to csv file with Python
Hashing data in R and Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Export and output files in Python
How to handle Japanese in Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Extract bigquery dataset and table list with python and output as CSV
[Introduction to Python] How to output a character string in a Print statement
How to import CSV and TSV files into SQLite with Python
[Python] How to add rows and columns to a table (pandas DataFrame)
How to use functions in separate files Perl and Python versions
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-