In [7]:
import pandas as pd
In [8]:
fruits = pd.Series(['Apple', 'Banana', 'Cherry', 'Orange'], name = "Fruit")
fruits
Out[8]:
In [9]:
qtys = pd.Series([5,7,2,9])
qtys
Out[9]:
In [11]:
# dictionary of Series
inventory = pd.DataFrame({ 'Fruit' : fruits, 'Qty' : qtys, 'Price': [2.99,1.99,3.99,2.99] })
inventory
Out[11]:
In [12]:
# column selection
inventory['Fruit']
Out[12]:
In [13]:
# as DataFrame
inventory[ ['Fruit'] ]
Out[13]:
In [14]:
#two columns in the list
inventory[ ['Fruit','Price'] ]
Out[14]:
In [15]:
# Boolean index
inventory['Qty'] >5
Out[15]:
In [16]:
# applying a boolean index to a dataframe
inventory[ inventory['Qty'] >5 ]
Out[16]:
In [20]:
#combining columns and filters
fruit_and_price_over5 = inventory[['Fruit','Price']][inventory['Qty'] >5 ]
fruit_and_price_over5
Out[20]:
In [19]:
# Confused? Too hot to handle??? Use variables!
large_qty = inventory[ inventory['Qty'] >5 ]
fruit_and_price_over5 = large_qty[ ['Fruit', 'Price' ] ]
fruit_and_price_over5
Out[19]:
In [ ]: