01: Building a pandas Cheat Sheet, Part 1
Use the csv I've attached to answer the following questions
In [2]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
df=pd.read_csv("/home/sean/Downloads/07-hw-animals.csv")
In [4]:
print(df.columns.values)
In [5]:
df.sort_values(by='length', ascending=False).head(3)
Out[5]:
In [6]:
df['animal'].value_counts()
Out[6]:
In [7]:
df['animal']=='dog'
#this only gives a list of true/false values
Out[7]:
In [8]:
dogs=df[df['animal']=='dog']
#this will give a datafram
In [9]:
dogs
Out[9]:
In [10]:
df['inches']=df['length']/2.54
df
Out[10]:
In [11]:
cats=df[df['animal']=='cat']
In [12]:
cats[cats['inches']>12]
Out[12]:
In [18]:
df[(df['animal']=='cat') & (df['inches']>12)]
Out[18]:
In [19]:
df[df['animal']=='dog'].mean()
Out[19]:
In [20]:
df[df['animal']=='cat'].mean()
Out[20]:
In [23]:
df.groupby('animal').describe()
Out[23]:
In [21]:
df.groupby('animal').mean()
Out[21]:
In [24]:
dogs=df[df['animal']=='dog']
In [25]:
dogs['length'].hist()
Out[25]:
In [26]:
df['length'].plot(kind='bar')
Out[26]:
In [27]:
df.plot(kind='bar', x='name', y='length')
Out[27]:
In [29]:
df.plot(kind='barh', x='name', y='length', legend=False)
Out[29]:
In [33]:
df[df['animal']=='cat'].sort_values(by='length').plot(kind='barh', x='name', y='length')
Out[33]:
In [ ]: