【问题标题】:TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]TypeError:无法使用 dtyped [float64] 数组和 [bool] 类型的标量执行“rand_”
【发布时间】:2020-06-24 13:03:11
【问题描述】:

我在 python pandas 中运行了如下命令:

q1_fisher_r[(q1_fisher_r['TP53']==1) & q1_fisher_r[(q1_fisher_r['TumorST'].str.contains(':1:'))]]

我收到以下错误:

TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]

我尝试使用的解决方案: error link.

相应地将代码更改为:

q1_fisher_r[(q1_fisher_r['TumorST'].str.contains(':1:')) & (q1_fisher_r[(q1_fisher_r['TP53']==1)])]

但我还是遇到了和TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]一样的错误

【问题讨论】:

  • 您的问题已得到解答here

标签: python pandas


【解决方案1】:

在以下设置中遇到了类似的问题,产生了相同的错误消息。对我来说非常简单的解决方案是将每个单独的条件放在括号中。应该知道,但想强调一下,以防其他人有同样的问题。

不正确的代码:

conditions = [
    (df['A'] == '15min' & df['B'].dt.minute == 15),  # Note brackets only surrounding both conditions together, not each individual condition
    df['A'] == '30min' & df['B'].dt.minute == 30,  # Note no brackets at all 
]

output = [
    df['Time'] + dt.timedelta(minutes = 45),
    df['Time'] + dt.timedelta(minutes = 30),
]

df['TimeAdjusted'] = np.select(conditions, output, default = np.datetime64('NaT'))

正确代码:

conditions = [
        (df['A'] == '15min') & (df['B'].dt.minute == 15),  # Note brackets surrounding each condition
        (df['A'] == '30min') & (df['B'].dt.minute == 30),  # Note brackets surrounding each condition
]
    
output = [
        df['Time'] + dt.timedelta(minutes = 45),
        df['Time'] + dt.timedelta(minutes = 30),
]

df['TimeAdjusted'] = np.select(conditions, output, default = np.datetime64('NaT'))

【讨论】:

    【解决方案2】:

    对于通过多个条件进行过滤,通过& 链接它们并通过boolean indexing 过滤:

    q1_fisher_r[(q1_fisher_r['TP53']==1) & q1_fisher_r['TumorST'].str.contains(':1:')]
                   ^^^^                        ^^^^
               first condition            second condition
    

    问题是这段代码返回过滤数据,所以不能按条件链接:

    q1_fisher_r[(q1_fisher_r['TumorST'].str.contains(':1:'))]
    

    类似的问题:

    q1_fisher_r[(q1_fisher_r['TP53']==1)]
    

    示例

    q1_fisher_r = pd.DataFrame({'TP53':[1,1,2,1], 'TumorST':['5:1:','9:1:','5:1:','6:1']})
    print (q1_fisher_r)
       TP53 TumorST
    0     1    5:1:
    1     1    9:1:
    2     2    5:1:
    3     1     6:1
    
    df = q1_fisher_r[(q1_fisher_r['TP53']==1) & q1_fisher_r['TumorST'].str.contains(':1:')]
    print (df)
       TP53 TumorST
    0     1    5:1:
    1     1    9:1:
    

    【讨论】:

    • 代码q1_fisher[q1_fisher['TumorST'].str.contains(':1:')]生成了10行,其中3行包含TP53。但是当我使用你的代码时:q1_fisher_r[(q1_fisher_r['TP53']==1) & q1_fisher_r['TumorST'].str.contains(':1:')]。它没有按预期为输出提供 3 行。它只是给出了标题
    • @user288925 - 你能为minimal, complete, and verifiable example添加一些示例数据吗?
    • @user288925 - 添加了示例数据,工作方式与预期的第一个条件和第二个条件类似
    • 它有效.. 我拿了你的数据并为我的数据尝试了类似的方法
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2021-05-24
    • 2016-11-10
    • 2013-12-18
    • 2020-03-02
    • 1970-01-01
    • 2016-01-29
    • 2023-03-27
    相关资源
    最近更新 更多