Python RuntimeWarning:在longlong\u标量中遇到无效值 我想做什么

Python RuntimeWarning:在longlong\u标量中遇到无效值 我想做什么,python,pandas,Python,Pandas,我想报告多个用户的每周拒绝率。我使用for循环遍历每月的数据集,以获得每个用户的数字。最后一个数据帧,rates,应该类似于: 描述 我有一个初始数据框(编号),它只包含接受、拒绝和审阅编号,我在其中添加了以下行和列: 行:总计,拒绝率 列:总计 以下是数字的样子: |---|--------|--------|--------|--------|-------------| | | Week 1 | Week 2 | Week 3 | Week 4 | Grand Total |

我想报告多个用户的每周拒绝率。我使用for循环遍历每月的数据集,以获得每个用户的数字。最后一个数据帧,
rates
,应该类似于:

描述 我有一个初始数据框(
编号
),它只包含接受、拒绝和审阅编号,我在其中添加了以下行和列:

  • :总计,拒绝率
  • :总计
以下是
数字的样子:

|---|--------|--------|--------|--------|-------------|
|   | Week 1 | Week 2 | Week 3 | Week 4 | Grand Total | 
|---|--------|--------|--------|--------|-------------|
| 0 |  994   |  699   |  529   |   877  |     3099    | 
|---|--------|--------|--------|--------|-------------|
| 1 |   27   |   7    |    8   |   13   |      55     |
|---|--------|--------|--------|--------|-------------|
| 2 |  100   |   86   |   64   |   107  |      357    |
|---|--------|--------|--------|--------|-------------|
| 3 |  1121  |  792   |  601   |  997   |    3511     |
|---|--------|--------|--------|--------|-------------|
这些索引表示以下值:

  • 0-接受
  • 1-拒绝
  • 2-审查
  • 3-总计(接受+拒绝+审核)
我编写了两个预定义函数:

  • get\u declient\u rates(df)
    :在
    numbers
    数据框中按周获取下降率
  • copy(empty_df,data)
    :将所有数据传输到具有“double”标题的新数据帧(用于报告目的)
  • 这是我的代码,我将行和列添加到编号中,然后重新格式化:

    # Adding "Grand Total" column and rows
    totals = numbers.sum(axis=0) # column sum
    numbers = numbers.append(totals, ignore_index=True)
    grand_total = numbers.sum(axis=1) # row sum
    numbers.insert(len(numbers.columns), "Grand Total", grand_total)
    
    # Adding "Rejection Rate" and re-indexing numbers
    decline_rates = get_decline_rates(numbers)
    numbers = numbers.append(decline_rates, ignore_index=True)
    numbers.index = ["ACCEPT","REJECT","REVIEW","Grand Total","Rejection Rate"]
    
    # Creating a new df with report format requirements 
    final = pd.DataFrame(0, columns=numbers.columns, index=["User A"]+list(numbers.index))
    final.ix["User A",:] = final.columns
    
    # Copying data from numbers to newly formatted df
    copy(final,numbers) 
    
    # Append final df of this user to the final dataframe
    rates = rates.append(final)
    
    我正在使用Python 3.5.2和熊猫0.19.2。如果有帮助,以下是初始数据集的外观:

    我对日期列进行了重新采样,以按周获取数据

    怎么了 有趣的是,代码运行良好,我在
    rates
    中获得了所有必需的信息。但是,我看到了以下警告信息:

    RuntimeWarning:在longlong\u标量中遇到无效值

    如果我分解代码并逐行运行,则不会显示此消息。甚至这个消息看起来都很奇怪(longlong_标量是什么意思?)有人知道这个警告消息是什么意思吗?是什么导致了它

    更新:

    我刚刚运行了一个类似的脚本,它接收完全相同的输入并生成类似的输出(除了我得到的是每日拒绝率而不是每周拒绝率)。我得到了相同的运行时警告,但给出了更多信息:

    RuntimeWarning:在longlong\u标量中遇到无效值

    rej_rate=str(整数((第九列[1]/第九列[3])*100))+“%””

    我怀疑当我试图用预定义的函数“get_Decept_rates(df)
    计算递减率时,一定是出了什么问题。这可能是由于值的数据类型造成的吗?输入df上的所有列,
    数字
    ,都是
    int64

    这是我预定义函数的代码(输入,
    numbers
    ,可在说明下找到):


    我有相同的RuntimeWarning,在查看数据之后,这是因为一个空除法。我没有时间查看您的样本,但您可以查看id=0或其他一些记录,其中可能会出现空除法或类似情况。

    您的输入是什么样的?欢迎来到Stackoverflow!如果你按照这篇文章提问,我会帮你很大的忙@斯科特伯顿你好!谢谢分享这篇文章,我的问题不完整。我编辑了这篇文章,以包含数据输入的外观。可在此处访问完整的数据集:
    # Description: Get rejection rates for all weeks.
    # Parameters: Pandas Dataframe with ACCEPT, REJECT, REVIEW count by week.
    # Output: Pandas Series with rejection rates for all days in input df.
    def get_decline_rates(df):
        decline_rates = []
        for i in range(len(df.columns)):
            col = df.ix[:,i]
    
            try:
                rej_rate = str(int(round((col[1]/col[3])*100))) + "%"
            except ValueError:
                rej_rate = "0%"
    
            decline_rates.append(rej_rate)
    
        return pd.Series(decline_rates, index=df.columns)