我在做什么
我想报告多个用户的每周拒绝率。我使用for循环来查看每月数据集以获取每个用户的数字。最后的数据帧,rates
,应该是这个样子:RuntimeWarning:longlong_scalars中遇到的无效值
说明
我有一个初步的数据框(numbers
),仅包含接受,拒绝和审查的数字,在这里我添加了这些行和列:
- 行:总计,拒绝率
- 分列:总计
这里的numbers
看看怎么样:
|---|--------|--------|--------|--------|-------------|
| | 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 TOTAL(接受+拒绝+评论)
numbers
数据帧中获得的下降率按周: -
我写了2预先定义的功能:
get_decline_rates(df)
。copy(empty_df, data)
:将所有数据传输到具有“双”标题的新数据帧(用于报告目的)。
这里是我的代码,我添加的行和列numbers
,然后重新格式化:
# 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_scalars
如果我打破了代码和行线运行它遇到无效值,不会出现此消息。即使这个消息看起来很奇怪(甚至是什么意思?)有人知道这个警告信息是什么意思,是什么造成的?
UPDATE:
我只是跑了类似的脚本,需要在完全相同的输入,并产生一个相似的输出(除了我得到每天的废品率,而不是每周一次)。我得到同样的警告运行时,除了更多的信息给出:
RuntimeWarning:在longlong_scalars
rej_rate = STR(INT(圆((col.ix [1] /col.ix遇到无效值3])*“%”
我怀疑当我试图用我的预定义函数get_decline_rates(df)
来计算下降速率时,肯定出了什么问题。这可能是由于价值的dtype?输入df,numbers
上的所有列都是int64
。
下面是我的预先定义的函数的代码(输入,numbers
,可在说明找到):
# 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)
你的输入是什么样的?欢迎来到Stackoverflow!如果你按照这篇文章提出问题,我会帮助你很多。 https://stackoverflow.com/help/mcve –
@ScottBoston您好!感谢分享这篇文章,我的问题并不完整。我编辑了这篇文章,以包括数据输入的外观。完整的数据集可以在这里访问:https://drive.google.com/file/d/0B9aicFjOCOKhR1VtZTVqa0FsM0U/view?usp=sharing –