Skip to content

Instantly share code, notes, and snippets.

@wrobstory
Created June 9, 2015 04:38
Show Gist options
  • Save wrobstory/500036b2544a1052dcf0 to your computer and use it in GitHub Desktop.
Save wrobstory/500036b2544a1052dcf0 to your computer and use it in GitHub Desktop.
Pandas Loc
zero_to_ten = loans.loc[(loans['funded_amnt'] >= 0) &
(loans['funded_amnt'] < 10000)]
ten_to_twenty = loans.loc[(loans['funded_amnt'] >= 10000) &
(loans['funded_amnt'] <= 20000)]
ten_to_twenty = loans[loans['funded_amnt'] >= 200000]
@TomAugspurger
Copy link

Depends a bit on what you're doing afterwards. Do you need them as 3 separate series? I'd maybe use

loans['amt_group'] = pd.cut(funded_amnt, bins=(0, 10000, 20000), include_lowest=True))

And then do grouped operations from there. If you do need the 3 separate Series, .query looks cleaner to me:

zero_to_ten = loans.query('0 <= funded_amnt < 10000')
ten_to_twenty = loans.query('10000 <= funded_amnt < 20000')
ten_to_twenty = loans.query('funded_amnt >= 200000')

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