Gharam

Exploring Data in Pandas Using GroupBy

May 9th, 2021
423
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Importing and Parsing
  2. >>>Import pandas as pd
  3. df = pd.read_csv('file_name.csv')
  4.  
  5. #Checking the mean of all attributes (columns) in a data set
  6. df.mean()
  7.  
  8. #Finding the mean of attributes when holding one attribute as an index
  9. _I.e: Here, I want to find the mean of all the other attributes with each occurence of the petal length attribute._
  10. >>>df.groupby('petal_length').mean()
  11.  
  12. #We can even add multiple entries to hold as an index
  13. >>>df.groupby(['petal_length', 'color']).mean()
  14.  
  15. #If we don't want the attributes we choose to be made as an index, we can use as_index=false:
  16. >>>df.groupby(['petal_length', 'color'], as_index=False).mean()
  17.  
  18. #And finally, if we are interested in only one attribute (column) we can index it as follows:
  19. >>>df.groupby(['petal_length', 'color'], as_index=False)['petal_width'].mean()
  20.  
  21.  
RAW Paste Data