search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
chevron_leftCreating DataFrames Cookbook
Combining multiple Series into a DataFrameCombining multiple Series to form a DataFrameConverting a Series to a DataFrameConverting list of lists into DataFrameConverting list to DataFrameConverting percent string into a numeric for read_csvConverting scikit-learn dataset to Pandas DataFrameConverting string data into a DataFrameCreating a DataFrame from a stringCreating a DataFrame using listsCreating a DataFrame with different type for each columnCreating a DataFrame with empty valuesCreating a DataFrame with missing valuesCreating a DataFrame with random numbersCreating a DataFrame with zerosCreating a MultiIndex DataFrameCreating a Pandas DataFrameCreating a single DataFrame from multiple filesCreating empty DataFrame with only column labelsFilling missing values when using read_csvImporting DatasetImporting tables from PostgreSQL as Pandas DataFramesInitialising a DataFrame using a constantInitialising a DataFrame using a dictionaryInitialising a DataFrame using a list of dictionariesInserting lists into a DataFrame cellKeeping leading zeroes when using read_csvParsing dates when using read_csvPreventing strings from getting parsed as NaN for read_csvReading data from GitHubReading file without headerReading large CSV files in chunksReading n random lines using read_csvReading space-delimited filesReading specific columns from fileReading tab-delimited filesReading the first few lines of a file to create DataFrameReading the last n lines of a fileReading URL using read_csvReading zipped csv file as a DataFrameRemoving Unnamed:0 columnResolving ParserError: Error tokenizing dataSaving DataFrame as zipped csvSkipping rows without skipping header for read_csvSpecifying data type for read_csvTreating missing values as empty strings rather than NaN for read_csv
check_circle
Mark as learned
thumb_up
0
thumb_down
1
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Removing Unnamed:0 column in Pandas

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

We can get an unwanted column named Unnamed:0 when creating a DataFrame from a csv file using the read_csv(~) method.

Suppose we have the following text file called sample.csv, which contains some comma-separated data:

,A,B,C
0,3,4,5
1,6,7,8

To create a DataFrame without Unnamed:0, we can pass index_col=0 to our read_csv(~) call:

import pandas as pv
df = pd.read_csv('sample.csv', index_col=0)
print(df)
A B C
0 3 4 5
1 6 7 8

If we do not specify index_col=0, the first column in the csv file is treated as the first column in the DataFrame and not the index. As there is no text (i.e. column name) before the first comma on the first line of the csv file, the resulting DataFrame column is given the name Unnamed:0. By passing index_col=0 we tell Pandas that the first column in the csv file is the index for the DataFrame.

When writing Pandas DataFrame as a csv

If the csv file you are reading was generated from a Pandas DataFrame, then we can remove the index label when exporting the DataFrame as a csv:

df = pd.DataFrame({'A':[3,4],'B':[5,6]})
df.to_csv('test.csv', index_label=False)

This will create the following csv file:

A,B
0,3,5
1,4,6

Now, we can read this csv file without having to specify index_col=0:

df = pd.read_csv('test.csv')
df
A B
0 3 5
1 4 6
robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
1
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!