[英]Modifying the date column calculation in pandas dataframe
我有一個看起來像這樣的 dataframe
我需要調整 34 號條目的time_in_weeks
列。 當存在具有不同rma_created_date
的重復uniqueid
時,這意味着發生了一些故障。 需要更改 34 以計算新的最近rma_created_date
(在本例中為 2020-10-15)之間的周數並減去上述行rma_processed_date
的 rma_processed_date。
我希望這對我正在嘗試做的事情有意義。
到目前為止,我這樣做了
def clean_df(df):
'''
This function will fix the time_in_weeks column to calculate the correct number of weeks
when there is multiple failured for an item.
'''
# Sort by rma_created_date
df = df.sort_values(by=['rma_created_date'])
現在我需要執行上面描述的操作,但我對如何執行此操作有點困惑。 特別是考慮到我們可能有多個失敗,而不僅僅是 2 個。
我應該得到這樣的東西返回為 output
如您所見, 34
發生了什么,它被更改為2020-10-15
和2020-06-26
之間的周數
這是另一個包含更多行的示例
使用建議的表達式
df['time_in_weeks']=np.where(df.uniqueid.duplicated(keep='first'),df.rma_processed_date.dt.isocalendar().week.sub(df.rma_processed_date.dt.isocalendar().week.shift(1)),df.time_in_weeks)
我明白了
最后說明:如果日期是 1900 年 1 月 1 日,則不要執行任何計算。
問題不是很清楚。 如果我解釋錯誤,很高興糾正。
嘗試使用np.where(condition, choiceif condition, choice ifnotcondition)
#Coerce dates into datetime
df['rma_processed_date']=pd.to_datetime(df['rma_processed_date'])
df['rma_created_date']=pd.to_datetime(df['rma_created_date'])
#Solution
df['time_in_weeks']=np.where(df.uniqueid.duplicated(keep='first'),df.rma_created_date.sub(df.rma_processed_date),df.time_in_weeks)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.