問題描述
我有一個帶有字母數(shù)字鍵的數(shù)據(jù)框,我想將其保存為 csv 并稍后讀回.由于各種原因,我需要將此鍵列顯式讀取為字符串格式,我有嚴格數(shù)字的鍵,甚至更糟,例如:1234E5,Pandas 將其解釋為浮點數(shù).這顯然使密鑰完全無用.
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.
問題是,當(dāng)我為數(shù)據(jù)框或其任何列指定字符串 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)
數(shù)據(jù)框如下:
A B
1A 0.209059 0.275554
1B 0.742666 0.721165
然后我是這樣讀的:
df_read = pd.read_csv(savefile, dtype=str, index_col=0)
結(jié)果是:
A B
B ( <
這是我的電腦問題,還是我在這里做錯了什么,或者只是一個錯誤?
Is this a problem with my computer, or something I'm doing wrong here, or just a bug?
推薦答案
更新:這有 已修復(fù):從 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
.
使用對象數(shù)據(jù)類型:
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
或者更好,只是不要指定數(shù)據(jù)類型:
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
但是繞過類型嗅探器并真正返回 only 字符串需要使用 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
是等于或大于您的總列數(shù)的某個數(shù)字.
where 100
is some number equal or greater than your total number of columns.
最好避免使用 str dtype,例如參見 這里.
這篇關(guān)于Pandas 將 csv 讀取為字符串類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!