我有個問題。我在計算第二天什么時候到。我想考慮到,如果今天的日期是星期六,它應該從 減去 -1 lastindays,因為星期天沒有包裹到達。我試過這個,但我不知道如何在我的 Apply 函式中指定兩個值:lastindays應該操作和回傳哪個值week,以及 if 陳述句發生在哪個值上。
如您所見,如果是星期六,該列lastindays應該只包含少一天。fromtDate并且新列lastindays_cleaned包含正確的值
資料框
customerId fromDate otherInformation
0 1 2021-02-22 Cat
1 1 2021-02-20 Dog
2 1 2021-03-18 Elefant
3 1 2021-03-18 Cat
4 1 2021-03-18 Cat
5 1 2021-03-22 Cat
6 1 2021-02-10 Cat
7 1 2021-09-07 Cat
8 1 None Elefant
9 1 2022-01-18 Fish
10 2 2021-05-17 Fish
代碼
import pandas as pd
d = {'customerId': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2],
'fromDate': ['2021-02-22','2021-02-20', '2021-03-18','2021-03-18', '2021-03-18', '2021-03-22',
'2021-02-10', '2021-09-07', None, '2022-01-18', '2021-05-17'],
'otherInformation': ['Cat', 'Dog', 'Elefant', 'Cat', 'Cat','Cat', 'Cat', 'Cat', 'Elefant', 'Fish', 'Fish']
}
df = pd.DataFrame(data=d)
print(df)
df['fromDate'] = pd.to_datetime(df['fromDate'], errors='coerce')
#df['lastindays'] = df['fromDate'] - df.sort_values('fromDate').groupby('customerId')['fromDate'].shift()
#print(df)
df['fromDate'] = pd.to_datetime(df['fromDate'], errors='coerce')
df['lastindays'] = df['fromDate'] - df.sort_values('fromDate').drop_duplicates(['customerId','fromDate']).groupby('customerId')['fromDate'].shift()
df['lastindays'] = df.groupby(['customerId','fromDate'])['lastindays'].ffill()
df = df[df['lastindays'].notna()]
df['lastindays_int'] = df['lastindays'].dt.days.astype('int')
df['week'] = df['fromDate'].dt.day_of_week
df['lastindays'] = df['lastindays'].apply(lambda x: x-1 if x == 5 else x)
df
我有的
customerId fromDate otherInformation lastindays lastindays_int week
0 1 2021-02-22 Cat 2 days 2 0
1 1 2021-02-20 Dog 10 days 10 5
2 1 2021-03-18 Elefant 24 days 24 3
3 1 2021-03-18 Cat 24 days 24 3
4 1 2021-03-18 Cat 24 days 24 3
5 1 2021-03-22 Cat 4 days 4 0
7 1 2021-09-07 Cat 169 days 169 1
9 1 2022-01-18 Fish 133 days 133 1
我想要的是
customerId fromDate otherInformation lastindays lastindays_cleaned
0 1 2021-02-22 Cat 2 days 2
1 1 2021-02-20 Dog 10 days 9 # from 10 -> 4, changes because the 2021-02-20 was a Saturday
2 1 2021-03-18 Elefant 24 days 24
3 1 2021-03-18 Cat 24 days 24
4 1 2021-03-18 Cat 24 days 24
5 1 2021-03-22 Cat 4 days 4
7 1 2021-09-07 Cat 169 days 169
9 1 2022-01-18 Fish 133 days 133
uj5u.com熱心網友回復:
df['week'] = df['fromDate'].dt.dayofweek
def func_data(x):
if x == 5:
x -=1
return x
df['week'] = df['week'].apply(func_data)
輸出
customerId fromDate otherInformation lastindays lastindays_int week
0 1 2021-02-22 Cat 2 days 2 0
1 1 2021-02-20 Dog 10 days 10 4
2 1 2021-03-18 Elefant 24 days 24 3
3 1 2021-03-18 Cat 24 days 24 3
4 1 2021-03-18 Cat 24 days 24 3
5 1 2021-03-22 Cat 4 days 4 0
7 1 2021-09-07 Cat 169 days 169 1
9 1 2022-01-18 Fish 133 days 133 1
或使用您的變體,但不要使用 'dt.day_of_week',而是使用 'dt.dayofweek'。而且在應用中,您需要使用列'df ['week']'。
df['week'] = df['fromDate'].dt.dayofweek
df['week'] = df['week'].apply(lambda x: x-1 if x == 5 else x)
更新的解決方案
index = df[df['week']==5].index
df.loc[index, 'lastindays_int'] = df.loc[index, 'lastindays_int'] - 1
uj5u.com熱心網友回復:
您可以使用 axis =1 一次訪問整行。
def date_check(cell):
return pd.to_datetime(cell, format='%Y-%m-%d').strftime('%a') == 'Sat'
def claculate(row):
check = date_check(row['fromDate'])
if check:
row['week']-1
return row['week']
df['week'] = df['week'].astype(int)
df['week'] = df.apply(lambda row: claculate(row), axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/486556.html
