Python 熊猫中的不同 read_csv index_col = None / 0 / False
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29862864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different read_csv index_col = None / 0 / False in pandas
提问by markov zain
I used the read_csv command following below:
我使用了下面的 read_csv 命令:
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:
结果显示列Unnamed:0
,当我使用时它是相似的index_col=False
,但是当我使用时index_col=0
,结果如下:
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.
结果确实显示了该列Unnamed:0
,在这里我想问一下index_col=None
,index_col=0
, 和之间有什么区别index_col=False
,我已经阅读了this中的文档,但我仍然没有得到这个想法。
采纳答案by EdChum
UPDATE
更新
I think since version 0.16.1it will now raise an error if you try to pass True
for index_col
to avoid this ambiguity
我认为从0.16.1版本开始,如果您尝试传递True
forindex_col
以避免这种歧义,它现在会引发错误
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
.
很多人对此感到困惑,要指定列的序数索引,您应该在这种情况下传递 int 位置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.
默认值index_col=None
如上所示。
If we set index_col=0
we're explicitly stating to treat the first column as the index:
如果我们设置,index_col=0
我们明确声明将第一列视为索引:
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
:
如果我们通过,index_col=False
我们会得到与以下相同的结果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:
如果我们现在声明,index_col=None
我们会得到与未传递此参数时相同的行为:
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
:
有一个错误,如果你通过True
this 被错误地转换index_col=1
为True
正在转换为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