df 是一個包含ship_date、order_date 和cumulative_ordered 的csv 文件。cumulative_ordered 是在ship_date 之前每天添加的訂單總和。每個 ship_date 前有 30 天,而那些天數只計算一個 ship_date。在ship_date 2018-07-01之后,那么下一個ship_date將是2018-08-01,程序相同。我的問題是,當我計算前 30 天中每一天的累積排序的百分比平均值時,我無法獲得剩余天數(請參閱下面的最后一個代碼輸出)。我有以下代碼,它為我提供了 csv 文件中的 days_remaining,其中包含幾個不同的 ship_date 和 order_date,分別倒計時到每個單獨的 ship_date。df['days_remaining'] = pd.to_datetime(df['ship_date']).sub\(pd.to_datetime(df['order_date'])).dt.daysdf['difference'] = df['ship_date'] - df['order_date']df.head()輸出:ship_date Order_date cumulative_ordered days_remaining difference2018-07-01 2018-06-01 7 30 30 days2018-07-01 2018-06-02 10 29 29 days2018-07-01 2018-06-03 15 28 28 days2018-07-01 2018-06-04 30 28 27 days2018-07-01 2018-06-05 41 28 26 days然后我嘗試查找ship_date之前每一天的總訂購量m = df.groupby("difference").mean()m.head()這給了我這個輸出: cumulative ordered days_remainingdifference 0 days 352.458124 0.0 1 days 291.234747 1.0 2 days 244.122137 2.0 3 days 201.178765 3.0 4 days 190.153641 4.0當我嘗試通過運行以下代碼,根據從上面 0 天的cumulative_ordered 輸出填充的百分比來查找每天累積訂購的平均值時遇到了一個問題: v = m/m[m.index.days == 0].iloc[0] v.head() cumulative_ordered days_remainingdifference 0 days 1.000000 NaN1 days 0.891324 inf2 days 0.812534 inf3 days 0.752339 inf4 days 0.673745 infdays_remaining 更改為 NaN 和 inf .. 我怎樣才能保留它以便它仍然給我整數?
1 回答

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
的NaN
和inf
從通過劃分導致0.0
。
似乎您試圖僅將操作應用于cumulative_ordered
列,因此您應該為最后一個代碼塊運行它:
m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]
添加回答
舉報
0/150
提交
取消