<i id='e7ArV'><tr id='e7ArV'><dt id='e7ArV'><q id='e7ArV'><span id='e7ArV'><b id='e7ArV'><form id='e7ArV'><ins id='e7ArV'></ins><ul id='e7ArV'></ul><sub id='e7ArV'></sub></form><legend id='e7ArV'></legend><bdo id='e7ArV'><pre id='e7ArV'><center id='e7ArV'></center></pre></bdo></b><th id='e7ArV'></th></span></q></dt></tr></i><div id='e7ArV'><tfoot id='e7ArV'></tfoot><dl id='e7ArV'><fieldset id='e7ArV'></fieldset></dl></div>

      <small id='e7ArV'></small><noframes id='e7ArV'>

          <bdo id='e7ArV'></bdo><ul id='e7ArV'></ul>
      1. <tfoot id='e7ArV'></tfoot><legend id='e7ArV'><style id='e7ArV'><dir id='e7ArV'><q id='e7ArV'></q></dir></style></legend>

      2. Pandas 将 csv 读取为字符串类型

        时间:2023-08-04

        1. <tfoot id='k8tSt'></tfoot>

            <tbody id='k8tSt'></tbody>
          <i id='k8tSt'><tr id='k8tSt'><dt id='k8tSt'><q id='k8tSt'><span id='k8tSt'><b id='k8tSt'><form id='k8tSt'><ins id='k8tSt'></ins><ul id='k8tSt'></ul><sub id='k8tSt'></sub></form><legend id='k8tSt'></legend><bdo id='k8tSt'><pre id='k8tSt'><center id='k8tSt'></center></pre></bdo></b><th id='k8tSt'></th></span></q></dt></tr></i><div id='k8tSt'><tfoot id='k8tSt'></tfoot><dl id='k8tSt'><fieldset id='k8tSt'></fieldset></dl></div>
              <bdo id='k8tSt'></bdo><ul id='k8tSt'></ul>
                <legend id='k8tSt'><style id='k8tSt'><dir id='k8tSt'><q id='k8tSt'></q></dir></style></legend>

                • <small id='k8tSt'></small><noframes id='k8tSt'>

                • 本文介绍了Pandas 将 csv 读取为字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个带有字母数字键的数据框,我想将其保存为 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.

                  使用对象数据类型:

                  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
                  

                  或者更好,只是不要指定数据类型:

                  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 是等于或大于您的总列数的某个数字.

                  where 100 is some number equal or greater than your total number of columns.

                  最好避免使用 str dtype,例如参见 这里.

                  这篇关于Pandas 将 csv 读取为字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:错误:'int' 对象不可下标 - Python 下一篇:将表示列表的字符串转换为实际的列表对象

                  相关文章

                  最新文章

                • <legend id='XeGDj'><style id='XeGDj'><dir id='XeGDj'><q id='XeGDj'></q></dir></style></legend>

                    <tfoot id='XeGDj'></tfoot>

                        <bdo id='XeGDj'></bdo><ul id='XeGDj'></ul>

                      <small id='XeGDj'></small><noframes id='XeGDj'>

                      <i id='XeGDj'><tr id='XeGDj'><dt id='XeGDj'><q id='XeGDj'><span id='XeGDj'><b id='XeGDj'><form id='XeGDj'><ins id='XeGDj'></ins><ul id='XeGDj'></ul><sub id='XeGDj'></sub></form><legend id='XeGDj'></legend><bdo id='XeGDj'><pre id='XeGDj'><center id='XeGDj'></center></pre></bdo></b><th id='XeGDj'></th></span></q></dt></tr></i><div id='XeGDj'><tfoot id='XeGDj'></tfoot><dl id='XeGDj'><fieldset id='XeGDj'></fieldset></dl></div>