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

    <legend id='P5I8F'><style id='P5I8F'><dir id='P5I8F'><q id='P5I8F'></q></dir></style></legend>
  1. <small id='P5I8F'></small><noframes id='P5I8F'>

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

    1. 阻止 Pandas 将 int 转换为 float

      时间:2023-09-14
    2. <small id='7XSBo'></small><noframes id='7XSBo'>

            • <bdo id='7XSBo'></bdo><ul id='7XSBo'></ul>
              <tfoot id='7XSBo'></tfoot>
              • <i id='7XSBo'><tr id='7XSBo'><dt id='7XSBo'><q id='7XSBo'><span id='7XSBo'><b id='7XSBo'><form id='7XSBo'><ins id='7XSBo'></ins><ul id='7XSBo'></ul><sub id='7XSBo'></sub></form><legend id='7XSBo'></legend><bdo id='7XSBo'><pre id='7XSBo'><center id='7XSBo'></center></pre></bdo></b><th id='7XSBo'></th></span></q></dt></tr></i><div id='7XSBo'><tfoot id='7XSBo'></tfoot><dl id='7XSBo'><fieldset id='7XSBo'></fieldset></dl></div>
                <legend id='7XSBo'><style id='7XSBo'><dir id='7XSBo'><q id='7XSBo'></q></dir></style></legend>
                  <tbody id='7XSBo'></tbody>
                本文介绍了阻止 Pandas 将 int 转换为 float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个 DataFrame.以下是两个相关的列:一个是 int 的列,另一个是 str 的列.

                I have a DataFrame. Two relevant columns are the following: one is a column of int and another is a column of str.

                我知道如果我将 NaN 插入 int 列,Pandas 会将所有 int 转换为 float 因为 int 没有 NaN 值.

                I understand that if I insert NaN into the int column, Pandas will convert all the int into float because there is no NaN value for an int.

                但是,当我将 None 插入 str 列时,Pandas 会将我的所有 int 转换为 float 为好.这对我来说没有意义 - 为什么我在第 2 列中输入的值会影响第 1 列?

                However, when I insert None into the str column, Pandas converts all my int to float as well. This doesn't make sense to me - why does the value I put in column 2 affect column 1?

                这是一个简单的工作示例(Python 2):

                Here's a simple working example (Python 2):

                import pandas as pd
                df = pd.DataFrame()
                df["int"] = pd.Series([], dtype=int)
                df["str"] = pd.Series([], dtype=str)
                df.loc[0] = [0, "zero"]
                print df
                print
                df.loc[1] = [1, None]
                print df
                

                输出是

                   int   str
                0    0  zero
                
                   int   str
                0  0.0  zero
                1  1.0   NaN
                

                有没有办法让输出如下:

                Is there any way to make the output the following:

                   int   str
                0    0  zero
                
                   int   str
                0    0  zero
                1    1   NaN
                

                不将第一列重铸为 int.

                • 我更喜欢使用 int 而不是 float 因为实际数据在该列是整数.如果没有解决方法,我只会使用 float.

                • I prefer using int instead of float because the actual data in that column are integers. If there's not workaround, I'll just use float though.

                我不喜欢重铸,因为在我的实际代码中,我不需要
                存储实际的dtype.

                I prefer not having to recast because in my actual code, I don't
                store the actual dtype.

                我还需要逐行插入数据.

                I also need the data inserted row-by-row.

                推荐答案

                如果你设置dtype=object,你的系列就可以包含任意数据类型:

                If you set dtype=object, your series will be able to contain arbitrary data types:

                df["int"] = pd.Series([], dtype=object)
                df["str"] = pd.Series([], dtype=str)
                df.loc[0] = [0, "zero"]
                print(df)
                print()
                df.loc[1] = [1, None]
                print(df)
                
                   int   str
                0    0  zero
                1  NaN   NaN
                
                  int   str
                0   0  zero
                1   1  None
                

                这篇关于阻止 Pandas 将 int 转换为 float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Python 中将字符串转换为枚举 下一篇:Python 的 coerce() 是做什么用的?

                相关文章

                最新文章

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

                2. <legend id='ndg5s'><style id='ndg5s'><dir id='ndg5s'><q id='ndg5s'></q></dir></style></legend>

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

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