How to drop "Unnamed: 0" column from DataFrame
There are situations when an Unnamed: 0 column in pandas comes when you are reading CSV file . The simplest solution would be to read the "Unnamed: 0" column as the index. So, what you have to do is to specify an index_col=[0] argument to read_csv() function, then it reads in the first column as the index.
pd.read_csv('file.csv', index_col=[0])
index_col=[0]
While you read csv file, if you set index_col=[0] you're explicitly stating to treat the first column as the index.
import pandas as pd
import numpy as np
from io import StringIO
df = pd.DataFrame(np.random.randn(5,3), columns=list('xyz'))
pd.read_csv(io.StringIO(df.to_csv()))
Unnamed: 0 x y z
0 0 -0.515264 -0.167118 0.695661
1 1 -1.226441 -1.232471 -1.087333
2 2 0.049460 0.904160 -0.923499
3 3 -1.411504 0.617604 2.769514
4 4 -0.847113 0.530300 1.254127
You can solve this issue by using index_col=0 in you read_csv() function.
pd.read_csv(io.StringIO(df.to_csv()), index_col=[0])
x y z
0 -0.279695 -0.217099 -1.208364
1 2.165306 -0.394201 -1.721362
2 1.436819 1.195225 1.570140
3 0.271943 0.940938 -0.230880
4 -1.044362 1.399811 0.725777
index=False
df.to_csv('file.csv', index=False)
In most cases, it is caused by your to_csv() having been saved along with an "Unnamed: 0" index. You could have avoided this mistakes in the first place by using "index=False" if the output CSV was created in DataFrame.
df = pd.DataFrame(np.random.randn(5,3), columns=list('xyz'))
pd.read_csv(io.StringIO(df.to_csv(index=False)))
x y z
0 1.025470 0.799474 0.078352
1 0.295817 0.115101 0.386230
2 1.306253 -0.988141 1.123844
3 -1.764021 -1.214755 0.592631
4 0.404928 -1.562189 0.921208

Using regex
You can get ride of all Unnamed columns from your DataFrame by using regex.
df.drop(df.filter(regex="Unname"),axis=1, inplace=True)
Finally, you can simply delete that column using: del df['column_name'] .
del df['column_name']
Related Topics
- ImportError: No module named pandas
- What is SettingWithCopyWarning?
- UnicodeDecodeError while reading CSV file
- How to fix CParserError: Error tokenizing data
- ValueError: cannot reindex from a duplicate axis
- ValueError: cannot convert float NaN to integer
- ValueError: Unknown label type: 'unknown'
- ValueError: Length of values does not match length of index
- ValueError: The truth value of an array with more than..