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

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

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

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

        <tfoot id='tm8ko'></tfoot>
      1. 将 pandas 数据框列导入为字符串而不是 int

        时间:2023-08-04
        <tfoot id='nxD8c'></tfoot>
              <tbody id='nxD8c'></tbody>
            • <legend id='nxD8c'><style id='nxD8c'><dir id='nxD8c'><q id='nxD8c'></q></dir></style></legend>

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

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

                <bdo id='nxD8c'></bdo><ul id='nxD8c'></ul>
                1. 本文介绍了将 pandas 数据框列导入为字符串而不是 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想将以下 csv 作为字符串而不是 int64 导入.Pandas read_csv 自动将其转换为 int64,但我需要此列作为字符串.

                  I would like to import the following csv as strings not as int64. Pandas read_csv automatically converts it to int64, but I need this column as string.

                  ID
                  00013007854817840016671868
                  00013007854817840016749251
                  00013007854817840016754630
                  00013007854817840016781876
                  00013007854817840017028824
                  00013007854817840017963235
                  00013007854817840018860166
                  
                  
                  df = read_csv('sample.csv')
                  
                  df.ID
                  >>
                  
                  0   -9223372036854775808
                  1   -9223372036854775808
                  2   -9223372036854775808
                  3   -9223372036854775808
                  4   -9223372036854775808
                  5   -9223372036854775808
                  6   -9223372036854775808
                  Name: ID
                  

                  不幸的是,使用转换器会得到相同的结果.

                  Unfortunately using converters gives the same result.

                  df = read_csv('sample.csv', converters={'ID': str})
                  df.ID
                  >>
                  
                  0   -9223372036854775808
                  1   -9223372036854775808
                  2   -9223372036854775808
                  3   -9223372036854775808
                  4   -9223372036854775808
                  5   -9223372036854775808
                  6   -9223372036854775808
                  Name: ID
                  

                  推荐答案

                  只是想重申这将适用于 pandas >= 0.9.1:

                  Just want to reiterate this will work in pandas >= 0.9.1:

                  In [2]: read_csv('sample.csv', dtype={'ID': object})
                  Out[2]: 
                                             ID
                  0  00013007854817840016671868
                  1  00013007854817840016749251
                  2  00013007854817840016754630
                  3  00013007854817840016781876
                  4  00013007854817840017028824
                  5  00013007854817840017963235
                  6  00013007854817840018860166
                  

                  我也在创建一个关于检测整数溢出的问题.

                  I'm creating an issue about detecting integer overflows also.

                  在此处查看解决方案:https://github.com/pydata/pandas/issues/2247

                  更新,因为它可以帮助他人:

                  Update as it helps others:

                  要将所有列作为str,可以这样做(来自评论):

                  To have all columns as str, one can do this (from the comment):

                  pd.read_csv('sample.csv', dtype = str)
                  

                  要将大多数或选择性列作为str,可以这样做:

                  To have most or selective columns as str, one can do this:

                  # lst of column names which needs to be string
                  lst_str_cols = ['prefix', 'serial']
                  # use dictionary comprehension to make dict of dtypes
                  dict_dtypes = {x : 'str'  for x in lst_str_cols}
                  # use dict on dtypes
                  pd.read_csv('sample.csv', dtype=dict_dtypes)
                  

                  这篇关于将 pandas 数据框列导入为字符串而不是 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Python 中将带点和逗号的字符串转换为浮点 下一篇:错误:'int' 对象不可下标 - Python

                  相关文章

                  最新文章

                2. <tfoot id='A7qTJ'></tfoot>

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

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

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