Advertisement
furas

Python - pandas - selecting & groupby

Jun 12th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement