Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different read_csv index_col = None / 0 / False in pandas

Tags:

I used the read_csv command following below:

    In [20]:
    dataframe = pd.read_csv('D:/UserInterest/output/ENFP_0719/Bookmark.csv', index_col=None)
    dataframe.head()
    Out[20]:
    Unnamed: 0  timestamp   url visits
    0   0   1.404028e+09    http://m.blog.naver.com/PostView.nhn?blogId=mi...   2
    1   1   1.404028e+09    http://m.facebook.com/l.php?u=http%3A%2F%2Fblo...   1
    2   2   1.404028e+09    market://details?id=com.kakao.story 1
    3   3   1.404028e+09    https://story-api.kakao.com/upgrade/install 4
    4   4   1.403889e+09    http://m.cafe.daum.net/WorldcupLove/Knj/173424...   1

The result shows column Unnamed:0 and it is simillar when I used index_col=False, but when I used index_col=0, the result is following below:

dataframe = pd.read_csv('D:/UserInterest/output/ENFP_0719/Bookmark.csv', index_col=0)
dataframe.head()
Out[21]:
timestamp   url visits
0   1.404028e+09    http://m.blog.naver.com/PostView.nhn?blogId=mi...   2
1   1.404028e+09    http://m.facebook.com/l.php?u=http%3A%2F%2Fblo...   1
2   1.404028e+09    market://details?id=com.kakao.story 1
3   1.404028e+09    https://story-api.kakao.com/upgrade/install 4
4   1.403889e+09    http://m.cafe.daum.net/WorldcupLove/Knj/173424...   1

The result did show the column Unnamed:0, In here I want to ask, what is the difference between index_col=None, index_col=0, and index_col=False, I have read the documentation in this, but I still did not get the idea.

like image 817
markov zain Avatar asked Apr 25 '15 08:04

markov zain


1 Answers

UPDATE

I think since version 0.16.1 it will now raise an error if you try to pass True for index_col to avoid this ambiguity

ORIGINAL

A lot of people get confused by this, to specify the ordinal index of your column you should pass the int position in this case 0.

In [3]:

import io
import pandas as pd
t="""index,a,b
0,hello,pandas"""
pd.read_csv(io.StringIO(t))
​
Out[3]:
   index      a       b
0      0  hello  pandas

The default value is index_col=None as shown above.

If we set index_col=0 we're explicitly stating to treat the first column as the index:

In [4]:

pd.read_csv(io.StringIO(t), index_col=0)
Out[4]:
           a       b
index               
0      hello  pandas

If we pass index_col=False we get the same result as None:

In [5]:

pd.read_csv(io.StringIO(t), index_col=False)
Out[5]:
   index      a       b
0      0  hello  pandas

If we now state index_col=None we get the same behaviour as when we didn't pass this param:

In [6]:

pd.read_csv(io.StringIO(t), index_col=None)
Out[6]:
   index      a       b
0      0  hello  pandas

There is a bug where if you pass True this was erroneously being converted to index_col=1 as True was being converted to 1:

In [6]:

pd.read_csv(io.StringIO(t), index_col=True)
Out[6]:
       index       b
a               
0      hello  pandas

EDIT

For the case where you have a blank index column which is what you have:

In [7]:

import io
import pandas as pd
t=""",a,b
0,hello,pandas"""
pd.read_csv(io.StringIO(t))
​
Out[7]:
   Unnamed: 0      a       b
0           0  hello  pandas
In [8]:

pd.read_csv(io.StringIO(t), index_col=0)
Out[8]:
       a       b
0  hello  pandas
In [9]:

pd.read_csv(io.StringIO(t), index_col=False)
Out[9]:
   Unnamed: 0      a       b
0           0  hello  pandas
In [10]:

pd.read_csv(io.StringIO(t), index_col=None)
Out[10]:
   Unnamed: 0      a       b
0           0  hello  pandas
like image 127
EdChum Avatar answered Sep 21 '22 18:09

EdChum