Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pd.read_csv add column named "Unnamed: 0

Tags:

python

pandas

csv

I have a dataframe with 3 columns. I save with pd.to_csv(filename) and then re-open it with

pd.read_csv(filename, index_col=False)

But I get a dataframe with 4 columns, with the left-most column called

Unnamed:0

that is actually just the row number. How can I read the csv without it?

Thanks!

like image 856
okuoub Avatar asked Dec 31 '18 13:12

okuoub


People also ask

How do I name an unnamed column in pandas DataFrame?

rename( columns={0 :'new column name'}, inplace=True ) . There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' . With Pandas 1.0.

Why do pandas add unnamed columns?

An unnamed column in pandas comes when you are reading the CSV file using it. Sometimes we require to drop columns in the dataset that we are not required. It not only saves memory but is also helpful in analyzing the data efficiently.

How do I stop unnamed zeros?

Stopgap Solution: Filtering with str.That index_col=[0] fix easily solved this annoying problem of 'unnamed:0' and spares code from verbose reinventing the wheel.

What does index_col 0 mean?

if index_col is 0, in this case it means that "1" will be the index of the first column, "2" will be the index for the second column and so on. If you have any other doubts like this you can refer to the pandas documentation.


1 Answers

You should try:

pd.read_csv('file.csv', index_col=0)

index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names)

Example Dataset:

I have taken the dataset from google,So while i'm simply trying to import the data with pd.read_csv it shows the Unnamed: 0 as default.

>>> df = pd.read_csv("amis.csv")
>>> df.head()
   Unnamed: 0  speed  period  warning  pair
0           1     26       1        1     1
1           2     26       1        1     1
2           3     26       1        1     1
3           4     26       1        1     1
4           5     27       1        1     1

So, Just to avoid the the Unnamed: 0 we have to use index_col=0 and will get the nicer dataframe:

>>> df = pd.read_csv("amis.csv", index_col=0)
>>> df.head()
   speed  period  warning  pair
1     26       1        1     1
2     26       1        1     1
3     26       1        1     1
4     26       1        1     1
5     27       1        1     1

Note : So, to make it more explicit to understand when we say index_col=0, it placed the first column as the index in the dataFrame rather appearing as Unnamed: 0 .

Hope this will help.

like image 119
Karn Kumar Avatar answered Oct 06 '22 03:10

Karn Kumar