<i id='nyvT3'><tr id='nyvT3'><dt id='nyvT3'><q id='nyvT3'><span id='nyvT3'><b id='nyvT3'><form id='nyvT3'><ins id='nyvT3'></ins><ul id='nyvT3'></ul><sub id='nyvT3'></sub></form><legend id='nyvT3'></legend><bdo id='nyvT3'><pre id='nyvT3'><center id='nyvT3'></center></pre></bdo></b><th id='nyvT3'></th></span></q></dt></tr></i><div id='nyvT3'><tfoot id='nyvT3'></tfoot><dl id='nyvT3'><fieldset id='nyvT3'></fieldset></dl></div>
<legend id='nyvT3'><style id='nyvT3'><dir id='nyvT3'><q id='nyvT3'></q></dir></style></legend>
    1. <small id='nyvT3'></small><noframes id='nyvT3'>

      <tfoot id='nyvT3'></tfoot>

        • <bdo id='nyvT3'></bdo><ul id='nyvT3'></ul>

        pandas read_csv 列 dtype 设置为十进制但转换为字符串

        时间:2023-08-04
        • <bdo id='oTCqd'></bdo><ul id='oTCqd'></ul>
            <tbody id='oTCqd'></tbody>

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

              <tfoot id='oTCqd'></tfoot>

            1. <small id='oTCqd'></small><noframes id='oTCqd'>

                  本文介绍了pandas read_csv 列 dtype 设置为十进制但转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 pandas (v0.18.1) 从名为test.csv"的文件中导入以下数据:

                  I am using pandas (v0.18.1) to import the following data from a file called 'test.csv':

                  a,b,c,d
                  1,1,1,1.0
                  

                  我已将列 'c' 和 'd' 的 dtype 设置为 'decimal.Decimal' 但它们返回为类型 'str'.

                  I have set the dtype to 'decimal.Decimal' for columns 'c' and 'd' but instead they return as type 'str'.

                  import pandas as pd
                  import decimal as D
                  
                  df = pd.read_csv('test.csv', dtype={'a': int, 'b': float, 'c': D.Decimal, 'd': D.Decimal})
                  
                  for i, v in df.iterrows():
                      print(type(v.a), type(v.b), type(v.c), type(v.d))
                  

                  结果:

                  `<class 'int'> <class 'float'> <class 'str'> <class 'str'>`
                  

                  我还尝试在导入后显式转换为十进制,但没有成功(转换为浮点有效但不是十进制).

                  I have also tried converting to decimal explicitly after import with no luck (converting to float works but not decimal).

                  df.c = df.c.astype(float)
                  df.d = df.d.astype(D.Decimal)
                  for i, v in df.iterrows():
                      print(type(v.a), type(v.b), type(v.c), type(v.d))
                  

                  结果:

                  `<class 'int'> <class 'float'> <class 'float'> <class 'str'>`
                  

                  以下代码将str"转换为decimal.Decimal",所以我不明白为什么 pandas 的行为方式不同.

                  The following code converts a 'str' to 'decimal.Decimal' so I don't understand why pandas doesn't behave the same way.

                  x = D.Decimal('1.0')
                  print(type(x))
                  

                  结果:

                  `<class 'decimal.Decimal'>`
                  

                  推荐答案

                  我觉得你需要转换器:

                  import pandas as pd
                  import io
                  import decimal as D
                  
                  temp = u"""a,b,c,d
                             1,1,1,1.0"""
                  
                  # after testing replace io.StringIO(temp) to filename
                  df = pd.read_csv(io.StringIO(temp), 
                                   dtype={'a': int, 'b': float}, 
                                   converters={'c': D.Decimal, 'd': D.Decimal})
                  
                  print (df)
                         a    b  c    d
                      0  1  1.0  1  1.0
                  
                  for i, v in df.iterrows():
                      print(type(v.a), type(v.b), type(v.c), type(v.d))
                  
                      <class 'int'> <class 'float'> <class 'decimal.Decimal'> <class 'decimal.Decimal'>
                  

                  这篇关于pandas read_csv 列 dtype 设置为十进制但转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用空值将字符串转换为日期时间 - python, 下一篇:从数据框中获取价值

                  相关文章

                  最新文章

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

                      <tfoot id='O9DFO'></tfoot>

                    3. <small id='O9DFO'></small><noframes id='O9DFO'>

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