【问题标题】:Pandas None series has 0Pandas None 系列有 0
【发布时间】:2022-01-22 16:26:46
【问题描述】:

我想生成一个带有 null 值、int 类型的 pandas 系列。我用这个:

import pandas as pd
pd.Series(None, index=[1, 2, 3], dtype=int)

然后它返回

1    0
2    0
3    0
dtype: int64

我该怎么做才能返回一个值为空的系列?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    Int64 用于nullable integer type

    s = pd.Series(None, index=[1, 2, 3], dtype='Int64')
    print (s)
    1    <NA>
    2    <NA>
    3    <NA>
    dtype: Int64
    

    s = pd.Series(np.nan, index=[1, 2, 3], dtype=int)
    print (s)
    1   NaN
    2   NaN
    3   NaN
    dtype: float64 <- casting to float
    

    【讨论】:

    • 我的主要问题是我正在尝试从变量类型创建系列:import pandas as pd; a = 2; type_out = type(a); pd.Series(None, index=[1, 2, 3], dtype=type_out)
    • 我不知道如何从 int 到 int64、float 到 float64 以及所有其他组合
    • @DavidMasip - 这是不可能的type=intNoneNaN
    • @DavidMasip - 只能使用一些默认值,例如 0 而不是 None
    • @DavidMasip - 它返回浮点数
    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2020-02-04
    • 2019-08-12
    • 2019-04-18
    • 2019-08-10
    • 2019-01-16
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多