如何将数据框日期与保持工作日和假期分开的行和列的值进行比较
How to compare dataframe dates with the values of row and column keeping weekdays and holidays apart
我有一个代表这个的数据框:
我需要创建另一个列 'Mark'
,这就是它复杂的原因。
对于值 'C'
,执行日是 Sunday 8/11/2018
。第二天将是 Monday 9/11/2018
。
所以我需要计算 previous week
的 weekdays
值。对于这种情况,我需要计算 1/11/2018
、2/11/2018
、3/11/2018
、4/11/2018
和 5/11/2018
.
但是,如果 execution day
的 next day
是 Friday
或 Saturday
,我需要取前一周的值 'Friday'
和 'Saturday'
。例如,B
在 Thursday 12/11/2018'
上执行。后天是'Friday'。所以我需要计算前一周的Friday
和Saturday
的平均值,分别是6/11/2018
和7/11/2018
最初我没有 Day
列,我使用
添加了后记
df['Execution']=pd.to_datetime(df['Execution'])
df['Day']=df['Execution'].dt.weekday_name
如果 execution date
与 column dates
之一匹配,我可以打印一些东西。这是代码-
for j,row in df.iterrows():
x=str(row['Execution'])
x=x[slicing]
for i, val in enumerate (df.columns.values):
print(df.columns[i])
if i<l1:
val=str(val)
val=val[slicing]
if x==val: #Execution date matches column date
print('yay')
我正在尝试自学 python,我已经开始学习 pandas dataframe
。
但是,现在我迷路了,无法弄清楚继续进行的逻辑。谁能给我指路?
这是对我有用的代码,并附有解释:
for i,row in df.iterrows():
for j, val in enumerate (range(0,l1-1)): #l1 is the number of columns
#subtracted 1 to not take last column in account as I only need the dates
if df.columns[j+1]==row['Execution']: #to match the date of column execution,with the column dates
a=pd.to_datetime(df.columns[j+1+1])
a=a.day_name() #to convert the date in to weekday name
#As for friday I would need previous week's friday and saturday values.
#Therefore, I subtracted 7 and 8 to get the required value. For all the other days I calculated carefully this way so that I get the days right.
if (a=='Friday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)])/2
#df.iloc(row,column) was used to get the values right
markList.append(mark)
elif (a=='Saturday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-8)])/2
markList.append(mark)
elif (a=='Sunday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+MPDr.iloc[i,(j+1+1-4)]+df.iloc[i,(j+1+1-3)])/5
markList.append(mark)
elif (a=='Monday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+df.iloc[i,(j+1+1-4)]+df.iloc[i,(j+1+1-8)])/5
markList.append(mark)
elif (a=='Tuesday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)])/5
markList.append(mark)
elif (a=='Wednesday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)]+df.iloc[i,(j+1+1-10)])/5
markList.append(mark)
elif (a=='Thursday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)]+df.iloc[i,(j+1+1-10)]+df.iloc[i,(j+1+1-11)])/5
markList.append(mark)
df['mark']=markList #To add at the end of the dataframe
我有一个代表这个的数据框:
我需要创建另一个列 'Mark'
,这就是它复杂的原因。
对于值 'C'
,执行日是 Sunday 8/11/2018
。第二天将是 Monday 9/11/2018
。
所以我需要计算 previous week
的 weekdays
值。对于这种情况,我需要计算 1/11/2018
、2/11/2018
、3/11/2018
、4/11/2018
和 5/11/2018
.
但是,如果 execution day
的 next day
是 Friday
或 Saturday
,我需要取前一周的值 'Friday'
和 'Saturday'
。例如,B
在 Thursday 12/11/2018'
上执行。后天是'Friday'。所以我需要计算前一周的Friday
和Saturday
的平均值,分别是6/11/2018
和7/11/2018
最初我没有 Day
列,我使用
df['Execution']=pd.to_datetime(df['Execution'])
df['Day']=df['Execution'].dt.weekday_name
如果 execution date
与 column dates
之一匹配,我可以打印一些东西。这是代码-
for j,row in df.iterrows():
x=str(row['Execution'])
x=x[slicing]
for i, val in enumerate (df.columns.values):
print(df.columns[i])
if i<l1:
val=str(val)
val=val[slicing]
if x==val: #Execution date matches column date
print('yay')
我正在尝试自学 python,我已经开始学习 pandas dataframe
。
但是,现在我迷路了,无法弄清楚继续进行的逻辑。谁能给我指路?
这是对我有用的代码,并附有解释:
for i,row in df.iterrows():
for j, val in enumerate (range(0,l1-1)): #l1 is the number of columns
#subtracted 1 to not take last column in account as I only need the dates
if df.columns[j+1]==row['Execution']: #to match the date of column execution,with the column dates
a=pd.to_datetime(df.columns[j+1+1])
a=a.day_name() #to convert the date in to weekday name
#As for friday I would need previous week's friday and saturday values.
#Therefore, I subtracted 7 and 8 to get the required value. For all the other days I calculated carefully this way so that I get the days right.
if (a=='Friday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)])/2
#df.iloc(row,column) was used to get the values right
markList.append(mark)
elif (a=='Saturday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-8)])/2
markList.append(mark)
elif (a=='Sunday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+MPDr.iloc[i,(j+1+1-4)]+df.iloc[i,(j+1+1-3)])/5
markList.append(mark)
elif (a=='Monday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+df.iloc[i,(j+1+1-4)]+df.iloc[i,(j+1+1-8)])/5
markList.append(mark)
elif (a=='Tuesday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-5)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)])/5
markList.append(mark)
elif (a=='Wednesday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-6)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)]+df.iloc[i,(j+1+1-10)])/5
markList.append(mark)
elif (a=='Thursday'):
mark=(df.iloc[i,(j+1+1-7)]+df.iloc[i,(j+1+1-8)]+df.iloc[i,(j+1+1-9)]+df.iloc[i,(j+1+1-10)]+df.iloc[i,(j+1+1-11)])/5
markList.append(mark)
df['mark']=markList #To add at the end of the dataframe