SHARE
TWEET

Python - pandas - selecting & groupby

furas Jun 12th, 2018 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2.  
  3. df = pd.DataFrame({
  4.     'type':['coupe', 'combi', 'coupe', 'combi', 'coupe', 'combi'],
  5.     'rating':['A', 'A', 'B', 'B', 'A', 'C']
  6. })
  7.  
  8. # --- selection ---
  9.  
  10. new_df = df[ (df['type'] == 'coupe') & (df['rating'] == 'A') ]
  11.  
  12. print(new_df)
  13. print('rows number:', new_df.shape[0])
  14. print('rows number:', len(new_df))
  15.  
  16. # --- groups ---
  17.  
  18. groups = df.groupby(['type', 'rating'])
  19.  
  20. for values, data in groups:
  21.     print('---')
  22.     if values == ('coupe', 'A'):
  23.         print('*** My Favorite ***')
  24.     print('values:', values)
  25.     print('data:\n', data)
  26.     print('rows:', len(data))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top