[Python] Format when to_csv with pandas

I've been using the same thing over and over again, so make a note of it. It is used when storing data in pandas and saving it as to_csv in a file. Use this when you want to have a clean format for publishing, and when you want to have the same number of digits.

Format the float

--To specify the whole float, the argument of to_csv can be set as float_format ="% 10.4f " --You can use round (). --You can take an integer as an argument to Series and round it with a decimal point or the number of digits of the integer (-2 for the hundreds digit). --When using round for DataFrame, you can set independently for multiple columns by taking a dictionary as an argument. --To specify the format for each column, you can specify it with map (lambda x:'{format}'.format (x)). You can use {0: .1f} for one digit less than a few points per week, and {: .6e} for exponential notation.

import pandas as pd
df = pd.read_csv("latlon.csv")
df['val'] = df['val'].map(lambda x: '{0:.1f}'.format(x))
df.to_csv("out.csv", index=False, header=True, float_format='%11.6f')

And out.csv is

year,month,day,lat,lon,val
2012,6,16,80.862745,-39.834254,0.0
2012,6,16,80.862745,-39.502762,0.1

Is output as. The same content uses round to

df = df.round({"lat":7, "lon":7, "val":1})

Recommended Posts

[Python] Format when to_csv with pandas
When to_csv with Pandas, it became line by line
Read csv with python pandas
[Python] Change dtype with pandas
Error when playing with python
Format json with Vim (with python)
String format with Python% operator
[Python] Join two tables with pandas
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
When matplotlib doesn't work with python2.7
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Excel aggregation with Python pandas Part 1
Snippet when searching all bits with python
Precautions when solving DP problems with Python
Excel aggregation with Python pandas Part 2 Variadic
Python> Format specification when printing `%`> "% s% s"% ('Hello','World',)
[Easy Python] Reading Excel files with pandas
FizzBuzz with Python3
Python string format
Scraping with Python
My pandas (python)
Read and format a csv file mixed with comma tabs with Python pandas
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Python string format
format in python
python pandas notes
Excel with Python
Microcomputer with Python
Cast with python
Error when installing a module with Python pip
Recommended environment and usage when developing with Python
Automatic operation of Chrome with Python + Selenium + pandas
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Personal tips when doing various things with Python 3
[Python] How to read excel file with pandas
Precautions when dealing with control structures in Python 2.6
Automatically format Python code into PEP8-compliant code with Emacs
Note for formatting numbers with python format function
Investigation when import cannot be done with python
A memo when creating a python environment with miniconda
Character encoding when dealing with files in Python 3
[python] [vscode] When you get angry with space-tab-mixed
Create a Photoshop format file (.psd) with python
[Web development with Python] Precautions when saving cookies
Materials to read when getting started with Python
Convert strings to character-by-character list format with python
What are you using when testing with Python?
[Python] Customize Colormap when drawing graphs with matplotlib
Serial communication with Python
Zip, unzip with python