Different read_csv index_col = None / 0 / False in pandas

本文解释了如何使用Pandas库正确地从CSV文件中读取数据并设置索引列,包括不同参数如index_col的使用方法及其可能导致的误解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UPDATE

I think since version 0.16.1 it will now raise an error if you try to pass a boolean 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, what confuses people is that when there is no index column they pass False which is incorrect they should pass NoneFalse will evaluate to 0 hence the result you observe.

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 above due to False evaluating to 0:

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

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值