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