我在做什麼
我想報告多個用戶的每週拒絕率。我使用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 –