Pandas Selection Exercises

See also: Indexing and Selecting Data



In [ ]:
# 1. Import pandas as pd and read the CFPB CSV into a dataframe assigned variable 'df'.

In [ ]:
# 2. Select a single column via bracket notation []. What is the type of the column?

In [ ]:
# 3. Select two or more columns using double bracket notation (list inside of brackets).

In [ ]:
# 4. Use the dataframe's loc[] attribute to get the first row of the df by index.

In [ ]:
# 5. Use iloc[] to get the last row of the dataframe by number.

In [ ]:
# 6. # Use df[] notation to get a True/False series for 'Product == Mortgage'. Assign to 'mask', then show.

In [ ]:
# 7. Use boolean indexing by putting the mask inside df[] notation to filter out everything but mortgage.

In [ ]:
# 8. Do the mask operation in steps 6 and 7 in a single line.

In [ ]:
# 9. Use df[(condition_1) | (condition_2)] notation to get Mortgage and Credit card products.
# Hint: separate conditions must be wrapped in () (and use | or & like notation).

In [ ]:
# 10. Drop a row from your df.

In [ ]:
# 11. Drop a column from your df.

In [ ]:
# 12. Use your df's loc[] attribute to get specific rows and columns using loc[NUMBER, NUMBER] notation.

In [ ]:
# 13. User your dataframe's set_index() method to use a ['Product', 'Sub-product']. Then sort_index().

In [ ]:
# 14. Use loc[] with a number like 1000000, and add a row to the dataframe.

In [ ]:
# 15. Add a column to the dataframe named "Custom" that copies the values in another column.

In [ ]:
# 16. Select based on a condition using the loc[CONDITION, COLUMN_NAME] notation.

In [ ]:
# 17. Select using loc[] and a list of index values.

In [ ]:
# 18. Use the [CONDITIONAL] selection to get all complaints from 2015.