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

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

        <tfoot id='K9AHZ'></tfoot>
      1. 如何将 pandas 数据框的数据类型更改为具有定义格式的字符串?

        How do I change data-type of pandas data frame to string with a defined format?(如何将 pandas 数据框的数据类型更改为具有定义格式的字符串?)

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

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

                • 本文介绍了如何将 pandas 数据框的数据类型更改为具有定义格式的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我开始为此扯头发 - 所以我希望有人能提供帮助.我有一个使用 openpyxl 从 Excel 电子表格创建的 pandas DataFrame.生成的 DataFrame 如下所示:

                  I'm starting to tear my hair out with this - so I hope someone can help. I have a pandas DataFrame that was created from an Excel spreadsheet using openpyxl. The resulting DataFrame looks like:

                  print image_name_data
                       id           image_name
                  0  1001  1001_mar2014_report
                  1  1002  1002_mar2014_report
                  2  1003  1003_mar2014_report
                  
                  [3 rows x 2 columns]
                  

                  …具有以下数据类型:

                  print image_name_data.dtypes
                  id            float64
                  image_name     object
                  dtype: object
                  

                  问题在于 id 列中的数字实际上是标识号,我需要将它们视为字符串.我尝试使用以下方法将 id 列转换为字符串:

                  The issue is that the numbers in the id column are, in fact, identification numbers and I need to treat them as strings. I've tried converting the id column to strings using:

                  image_name_data['id'] = image_name_data['id'].astype('str')
                  

                  这看起来有点难看,但它确实产生了一个类型为object"而不是float64"的变量:

                  This seems a bit ugly but it does produce a variable of type 'object' rather than 'float64':

                  print image_name_data.dyptes
                  id            object
                  image_name    object
                  dtype: object
                  

                  但是,创建的字符串有一个小数点,如图:

                  However, the strings that are created have a decimal point, as shown:

                  print image_name_data
                         id           image_name
                  0  1001.0  1001_mar2014_report
                  1  1002.0  1002_mar2014_report
                  2  1003.0  1003_mar2014_report
                  
                  [3 rows x 2 columns]
                  

                  如何将 pandas DataFrame 中的 float64 列转换为具有给定格式的字符串(在本例中,例如 '%10.0f')?

                  How can I convert a float64 column in a pandas DataFrame to a string with a given format (in this case, for example, '%10.0f')?

                  推荐答案

                  我无法重现您的问题,但您是否尝试过先将其转换为整数?

                  I'm unable to reproduce your problem but have you tried converting it to an integer first?

                  image_name_data['id'] = image_name_data['id'].astype(int).astype('str')
                  

                  然后,关于您更一般的问题,您可以使用 map (在这个答案中).在你的情况下:

                  Then, regarding your more general question you could use map (as in this answer). In your case:

                  image_name_data['id'] = image_name_data['id'].map('{:.0f}'.format)
                  

                  这篇关于如何将 pandas 数据框的数据类型更改为具有定义格式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)

                        <bdo id='RoTsp'></bdo><ul id='RoTsp'></ul>
                          <tbody id='RoTsp'></tbody>

                        <legend id='RoTsp'><style id='RoTsp'><dir id='RoTsp'><q id='RoTsp'></q></dir></style></legend>

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

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