[PYTHON] What to do if "Unnamed: 0" is added in to_csv-> read_csv in pandas

The beginning of the matter

The incident happened when I simply performed CSV writing → reading in Python's pandas as follows.

import pandas as pd

#Define variable data and write to CSV file
data = pd.DataFrame({'name': ['Taro', 'Hanako', 'Jiro', 'Yuki'],
                      'Math': [80, 15, 90, 50],
                      'English': [80, 70, 50, 65],
                      'National language': [ 90, 60, 60,  60]})
print(data)
data.to_csv('a.csv')


#Read and output the exported CSV file
data = pd.read_csv('a.csv')
print(data)

When the above was executed, there was a difference in the output of each.

#First output result
name math English national language
0 Taro 80 80 90
1 Hanako 15 70 60
2 Jiro 90 50 60
3 Yuki 50 65 60


#Second output result
   Unnamed:0 name math English national language
0 0 Taro 80 80 90
1 1 Hanako 15 70 60
2 2 Jiro 90 50 60
3 3 Yuki 50 65 60

A mysterious column called ʻUnnamed: 0` has been added. Eliminate this.

What's happening?

In the above writing, the following CSV file was output.

,name,Math,English,National language
0,Taro,80,80,90
1,Hanako,15,70,60
2,Jiro,90,50,60
3,Yuki,50,65,60

An unexpected , has been added to the left of name on the first line. As a result, it seems that the first column is regarded as bearer.

approach

Either of the following can be used. You don't have to do both.

Supported by to_csv

It can be solved by specifying index as false as shown below.

data.to_csv('a.csv', index=False)

From the above, the following CSV file could be output.

name,Math,English,National language
0,Taro,80,80,90
1,Hanako,15,70,60
2,Jiro,90,50,60
3,Yuki,50,65,60

read_csv

Specify the index column as follows.

data = pd.read_csv('a.csv', index_col=0)

Recommended Posts

What to do if "Unnamed: 0" is added in to_csv-> read_csv in pandas
What to do when UnicodeDecodeError occurs during read_csv in pandas (pd.read_table ())
What to do if pipreqs results in UnicodeDecodeError
What to do if there is a decimal in python json .dumps
What to do if pip install fails in Xcode 5.1
What to do if a UnicodeDecodeError occurs in pip
What to do if abort is displayed when inputting camera video in OpenCV
What to do if pyenv is not enabled (zsh)
What to do if the progress bar is not displayed in tqdm of python
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do if ʻarguments [0] .scrollIntoView ();` fails in python selenium
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do if you can't log in as root
What to do if yum breaks
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do if GeoPandas installation fails (Fiona / GDAL error * Rtree installation method is also added)
What to do if you get a minus zero in Python
What to do if the inode is exhausted on EC2 Linux
What to do if you can't use the trash in Lubuntu 18.04.
What to do when the value type is ambiguous in Python?
[Pandas] What is set_option [How to use]
What to do when the result downloaded via scrapy is in English
What to do if you can't find PDO in Laravel or CakePHP
What to do if you can't use scikit grid search in Python
What to do if you get lost in file reference with FileNotFoundError
What to do if No Python documentation found for ... appears in pydoc
What to do if you can't install with pip in babun environment
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What to do when a warning message is displayed in pip list
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
What to do if PyInstaller3.5 gives an error in Python3.8 (TypeError: an integer is required (got type bytes))
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
What to do if you can't pipenv shell
Note: What to do if pip install fails
What to do if mod_fcgid cannot resolve UnicodeEncodeError
What to do if rails s doesn't work
What to do if pip cannot be installed
What to do if atom autocomplete-python doesn't work
What to do to get google spreadsheet in python
What to do if Docker-sync suddenly stops working
What to do if "amazon-linux-extras" → "No module named amazon_linux_extras"
[Python] What to do if an error occurs in pip (pyinstaller, pyautogui, etc.)
What to do if you get a "No versions found" error in pipenv
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
What to do if a Unicode Encode Error occurs in Sublime Text Python
What to do if you get "Python not configured." Using PyDev in Eclipse
What to do if NotADirectoryError: [Errno 20] Not a directory:'xdg-settings' appears in jupyter notebook
What to do if a version error occurs in the selenium Chrome driver
What to do if Japanese language support is not completely installed on Ubuntu 16.04
What to do if (base) is displayed at the beginning of the Mac terminal
What to do if ubuntu says your password is too short to change [passwd command] [ubuntu]
What to do if you can't pip install mysqlclient
curl: (60) What to do when Issuer certificate is invalid.
What to do when is not in the sudoers file.This incident will be reported.
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
What to do if you get `No kernel for language python found` in Hydrogen
What to do if SciPy installation fails on CentOS
UnicodeDecodeError in pandas read_csv
What to do when only the window is displayed and nothing is displayed in pygame Note
What to do if the print command itself causes an error in Maya python
What to do if you get an error when importing matplotlib in Python (Mac)