Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

python - Different read_csv index_col = None / 0 / False in pandas

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...