I am trying to find whether Date
falls in PromoInterval
in a data frame.
print dset1
Store Date PromoInterval
1760 2 2013-05-04 Jan,Apr,Jul,Oct
1761 2 2013-05-03 Jan,Apr,Jul,Oct
1762 2 2013-05-02 Jan,Apr,Jul,Oct
1763 2 2013-05-01 Jan,Apr,Jul,Oct
1764 2 2013-04-30 Jan,Apr,Jul,Oct
def func(a,b):
y = b.split(",")
z = {1:'Jan',2:'Feb',3:'Mar', 4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',
10:'Oct',11:'Nov',12:'Dec'}
return (z[a] in y)
dset1.apply(func, axis=1, args = (dset1['Date'].dt.month, dset1['PromoInterval']) )
Struck at below error:
dset1.apply(func, axis=1, args = (dset1['Date'].dt.month, >dset1['PromoInterval']) ) ('func() takes exactly 2 arguments (3 given)', u'occurred at index 1760')
Data set:
{'Date': {1760: Timestamp('2013-05-04 00:00:00'),
1761: Timestamp('2013-05-03 00:00:00'),
1762: Timestamp('2013-05-02 00:00:00'),
1763: Timestamp('2013-05-01 00:00:00'),
1764: Timestamp('2013-04-30 00:00:00')},
'PromoInterval': {1760: 'Jan,Apr,Jul,Oct',
1761: 'Jan,Apr,Jul,Oct',
1762: 'Jan,Apr,Jul,Oct',
1763: 'Jan,Apr,Jul,Oct',
1764: 'Jan,Apr,Jul,Oct'},
'Store': {1760: 2, 1761: 2, 1762: 2, 1763: 2, 1764: 2}}
I would start by formatting the text string of the month using a lambda function on the 'Date'
column:
df['Month'] = df['Date'].apply(lambda x: x.strftime('%b'))
Then I would fire a lambda function on axis=1
which means it operates on the x axis over the dataframe. Here I simply check if 'Month'
is in 'PromoInterval'
df[['PromoInterval', 'Month']].apply(lambda x: x[1] in x[0], axis=1)
1760 False
1761 False
1762 False
1763 False
1764 True
dtype: bool
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments