Pandas Unsuccessfully Modify -
df=pd.read_csv('./data/reward_original_91011.csv') ind0=df[df['datepay'].isnull()].index df.ix[ind0]['valuepay']=0 ind1=df[~df['datepay'].isnull()].index df['datepay'] = pd.to_datetime(df['datepay']) df['datedue'] = pd.to_datetime(df['datedue']) ind2=df.ix[ind1][((df.ix[ind1]['datepay']-df.ix[ind1]['datedue'])/np.timedelta64(1,'h')).astype(int)>24].index **df.ix[ind2]['valuepay']=0**
is there can give me tips? why can modify above bold code successfully?
i think need:
df = pd.read_csv('./data/reward_original_91011.csv', parse_dates=['datepay','datedue']) #get boolean mask nan mask = df['datepay'].isnull() #modify column valuepay mask df.loc[mask, 'valuepay'] = 0 #get mask mask1 = ((df['datepay'] - df['datedue']) / np.timedelta64(1,'h')) > 24 #inverted mask , mask1 -> modify valuepay df.loc[~mask & mask1, 'valuepay'] = 0
sample:
d = {'datedue': [pd.timestamp('2013-10-06 00:00:00'), pd.timestamp('2013-10-08 00:00:00'), pd.timestamp('2013-10-08 00:00:00'), pd.timestamp('2013-10-08 00:00:00')], 'datepay': [np.nan, pd.timestamp('2013-10-06 00:00:00'), pd.timestamp('2013-10-12 00:00:00'), pd.timestamp('2013-10-10 00:00:00')]} df = pd.dataframe(d) print (df) datedue datepay 0 2013-10-06 nat 1 2013-10-08 2013-10-06 2 2013-10-08 2013-10-12 3 2013-10-08 2013-10-10 #get boolean mask nan mask = df['datepay'].isnull() #modify column valuepay mask df.loc[mask, 'valuepay'] = 0 #get mask mask1 = ((df['datepay'] - df['datedue']) / np.timedelta64(1,'h')) > 24 #inverted mask , mask1 -> modify valuepay df.loc[~mask & mask1, 'valuepay'] = 0 print (df) datedue datepay valuepay 0 2013-10-06 nat 0.0 1 2013-10-08 2013-10-06 nan 2 2013-10-08 2013-10-12 0.0 3 2013-10-08 2013-10-10 0.0
Comments
Post a Comment