Stop Pandas from converting int to float

前端 未结 3 1305
旧时难觅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
    

提交回复
热议问题