Skip to content

Instantly share code, notes, and snippets.

@saraswatmks
Created June 25, 2017 07:45
Show Gist options
  • Save saraswatmks/4e5f5b56e4ab889a5bbf2b7d4e503aad to your computer and use it in GitHub Desktop.
Save saraswatmks/4e5f5b56e4ab889a5bbf2b7d4e503aad to your computer and use it in GitHub Desktop.
Pandas Update a Column Based on Range
table_1 = pd.DataFrame({'Slab_ID': np.repeat('1_1',19), 'ActCastLength':np.arange(1.1,3,0.1)})
table_2 = pd.DataFrame({'Slab_ID': ['1_1','1_1'], 'DefectStart_Y':[1.5,2.3],\
'DefectEnd_Y':[1.8,2.5], 'Defective':[1,1]})
for c in np.arange(2):
table_1['Defective' + str(c)] = table_1['ActCastLength'].between(table_2.loc[c,'DefectStart_Y'],\
table_2.loc[c, 'DefectEnd_Y'])
table_1['Defective' + str(c)] = [1 if x == True else 0 for x in table_1['Defective'+str(c)]]
table_1['Defective'] = table_1['Defective0'] + table_1['Defective1']
table_1.drop(['Defective0','Defective1'], inplace=True, axis=1)
@saraswatmks
Copy link
Author

Unprogammatic solution, but works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment