pandas 将csv读取为字符串类型 [英] Pandas reading csv as string type
问题描述
我有一个带有字母数字键的数据框,我想将其另存为csv并在以后读取.由于种种原因,我需要以字符串格式显式读取此键列,因此我使用的键严格地是数字的,甚至更糟,例如:1234E5,Pandas会将其解释为浮点数.显然,这使密钥完全无用.
I have a data frame with alpha-numeric keys which I want to save as a csv and read back later. For various reasons I need to explicitly read this key column as a string format, I have keys which are strictly numeric or even worse, things like: 1234E5 which Pandas interprets as a float. This obviously makes the key completely useless.
问题是,当我为数据框或其中的任何列指定字符串dtype时,我只会得到垃圾回收.我在这里有一些示例代码:
The problem is when I specify a string dtype for the data frame or any column of it I just get garbage back. I have some example code here:
df = pd.DataFrame(np.random.rand(2,2),
index=['1A', '1B'],
columns=['A', 'B'])
df.to_csv(savefile)
数据框如下:
A B
1A 0.209059 0.275554
1B 0.742666 0.721165
然后我像这样阅读它:
df_read = pd.read_csv(savefile, dtype=str, index_col=0)
结果是:
A B
B ( <
这是我的计算机是否有问题,或者我在这里做错了什么,还是只是一个错误?
Is this a problem with my computer, or something I'm doing wrong here, or just a bug?
推荐答案
更新:这具有已固定:从0.11.1开始,您传递str
/np.str
等同于使用object
.
Update: this has been fixed: from 0.11.1 you passing str
/np.str
will be equivalent to using object
.
使用对象dtype:
In [11]: pd.read_csv('a', dtype=object, index_col=0)
Out[11]:
A B
1A 0.35633069074776547 0.745585398803751
1B 0.20037376323337375 0.013921830784260236
或更妙的是,只是不指定dtype:
or better yet, just don't specify a dtype:
In [12]: pd.read_csv('a', index_col=0)
Out[12]:
A B
1A 0.356331 0.745585
1B 0.200374 0.013922
,但绕过嗅探器类型并真正返回仅 字符串需要对converters
的恶意使用:
but bypassing the type sniffer and truly returning only strings requires a hacky use of converters
:
In [13]: pd.read_csv('a', converters={i: str for i in range(100)})
Out[13]:
A B
1A 0.35633069074776547 0.745585398803751
1B 0.20037376323337375 0.013921830784260236
其中100
是等于或大于您的总列数的数字.
where 100
is some number equal or greater than your total number of columns.
最好避免使用str dtype,请参见例如此处.
这篇关于 pandas 将csv读取为字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!