Stop Pandas from converting int to float

前端 未结 3 1302
旧时难觅i
旧时难觅i 2020-12-25 12:52

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

I understa

相关标签:
3条回答
  • 2020-12-25 13:31

    As of pandas 1.0.0 I believe you have another option, which is to first use convert_dtypes. This converts the dataframe columns to dtypes that support pd.NA, avoiding the issues with NaN/None.

    ...
    
    df = df.convert_dtypes()
    df.loc[1] = [1, None]
    print(df)
    
    #   int   str
    # 0   0  zero
    # 1   1  NaN
    
    0 讨论(0)
  • 2020-12-25 13:37

    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
    
    0 讨论(0)
  • 2020-12-25 13:46

    If you use DataFrame.append to add the data, the dtypes are preserved, and you do not have to recast or rely on object:

    In [157]: df
    Out[157]:
       int   str
    0    0  zero
    
    In [159]: df.append(pd.DataFrame([[1, None]], columns=['int', 'str']), ignore_index=True)
    Out[159]:
       int   str
    0    0  zero
    1    1  None
    
    0 讨论(0)
提交回复
热议问题