Basic Pandas Exercises

See also: 10 Minutes to Pandas



In [3]:
# 1. import pandas and then import pandas under the alias pd

import pandas
import pandas as pd

In [4]:
# 2. Use the dir() function to inspect some the availible attributes of pd.

dir(pd)[:5]


Out[4]:
['Categorical', 'CategoricalIndex', 'DataFrame', 'DateOffset', 'DatetimeIndex']

In [6]:
# 3.  Create a dataframe from the CFPB .csv using pd.read_csv(); assign to variable 'df'.

df = pd.read_csv('data/cfpb_complaints_with_fictitious_data.csv')

In [7]:
# 4. Use the dataframe's head() method to get the first 5 samples of df.

df.head(5)


Out[7]:
Date received Product Sub-product Issue Sub-issue Consumer complaint narrative Company public response Company State ZIP code ... Consumer consent provided? Submitted via Date sent to company Company response to consumer Timely response? Consumer disputed? Complaint ID Consumer Claim Company Offer Amount Received
0 3/24/2015 Credit card NaN Other NaN Received Capital One charge card offer XXXX. A... NaN Capital One OH 440XX ... Consent provided Web 3/30/2015 Closed with explanation Yes No 1297939 332.63 217.18 130.22
1 3/23/2015 Debt collection Other (i.e. phone, health club, etc.) Improper contact or sharing of info Contacted me after I asked not to I do n't know how they got my cell number. I t... NaN CCS Financial Services, Inc. AR 727XX ... Consent provided Web 3/23/2015 Closed with explanation Yes No 1296593 54.79 41.48 49.14
2 3/23/2015 Credit card NaN Rewards NaN I 'm a longtime member of Charter One Bank/RBS... NaN Citizens Financial Group, Inc. MI 482XX ... Consent provided Web 3/23/2015 Closed with explanation Yes Yes 1296693 215.04 231.67 155.28
3 3/23/2015 Credit reporting NaN Incorrect information on credit report Information is not mine After looking at my credit report, I saw a col... Company chooses not to provide a public response Experian FL 331XX ... Consent provided Web 3/27/2015 Closed with non-monetary relief Yes No 1296955 3.31 6.30 4.59
4 3/23/2015 Debt collection Payday loan Improper contact or sharing of info Talked to a third party about my debt I received a call from a XXXX XXXX from XXXX @... NaN Big Picture Loans, LLC SC 291XX ... Consent provided Web 3/23/2015 Closed with explanation Yes No 1296727 73.99 76.75 46.02

5 rows × 21 columns


In [8]:
# 5. Use the dtypes attribute to see what datatypes and columns we have in dataframe 'df'.

df.dtypes


Out[8]:
Date received                    object
Product                          object
Sub-product                      object
Issue                            object
Sub-issue                        object
Consumer complaint narrative     object
Company public response          object
Company                          object
State                            object
ZIP code                         object
Tags                             object
Consumer consent provided?       object
Submitted via                    object
Date sent to company             object
Company response to consumer     object
Timely response?                 object
Consumer disputed?               object
Complaint ID                      int64
Consumer Claim                  float64
Company Offer                   float64
Amount Received                 float64
dtype: object

In [9]:
# 6. Use the len() function to look at the length of the dataframe.

len(df)


Out[9]:
125680

In [10]:
# 7. Use the dataframe's sample method to pull a random sample of 50 from the dataframe

df.sample(50)


Out[10]:
Date received Product Sub-product Issue Sub-issue Consumer complaint narrative Company public response Company State ZIP code ... Consumer consent provided? Submitted via Date sent to company Company response to consumer Timely response? Consumer disputed? Complaint ID Consumer Claim Company Offer Amount Received
57108 5/18/2016 Mortgage FHA mortgage Loan servicing, payments, escrow account NaN My loan was originally serviced by XXXX XXXX. ... Company has responded to the consumer and the ... MidFirst Bank MD 211XX ... Consent provided Web 5/18/2016 Closed with explanation Yes Yes 1929590 186088.48 107978.18 128430.44
26871 8/8/2015 Credit reporting NaN Incorrect information on credit report Account status I have XXXX XXXX account on report that is XXX... Company chooses not to provide a public response TransUnion Intermediate Holdings, Inc. TX 752XX ... Consent provided Web 8/8/2015 Closed with explanation Yes No 1509833 2.72 4.29 3.57
28134 9/4/2015 Debt collection I do not know Communication tactics Frequent or repeated calls Esser, James & Associates called my fathers ph... NaN Esser, James & Associates CA 917XX ... Consent provided Web 9/8/2015 Closed with explanation Yes No 1552300 56.24 53.54 64.24
26705 8/7/2015 Bank account or service Other bank product/service Deposits and withdrawals NaN on XXXX XXXX 2015 at XXXX I walked in to Bank ... Company chooses not to provide a public response Bank of America TN 371XX ... Consent provided Web 8/7/2015 Closed with explanation Yes No 1508111 79.86 63.35 42.31
35319 2/8/2016 Mortgage Conventional fixed mortgage Loan servicing, payments, escrow account NaN On or around XXXX XXXX, XXXX, I received a XXX... Company believes it acted appropriately as aut... Select Portfolio Servicing, Inc AZ 851XX ... Consent provided Web 2/8/2016 Closed with explanation Yes Yes 1778299 119001.57 -43404.74 70317.17
123739 10/20/2016 Mortgage Conventional fixed mortgage Loan servicing, payments, escrow account NaN I refinanced a mortgage with a local bank who ... Company has responded to the consumer and the ... Wells Fargo & Company AR 720XX ... Consent provided Web 10/20/2016 Closed with explanation Yes No 2172560 145269.14 168226.87 94565.75
1118 4/16/2015 Debt collection Other (i.e. phone, health club, etc.) Cont'd attempts collect debt not owed Debt was paid paid collection at XXXX XXXX/XXXX/2015 shows n... Company chooses not to provide a public response Diversified Consultants, Inc. NM 871XX ... Consent provided Web 4/16/2015 Closed with explanation Yes No 1333776 69.68 61.16 47.90
61197 5/22/2015 Debt collection Credit card Cont'd attempts collect debt not owed Debt is not mine We received a letter addressed to my husband X... NaN Capital One MD 208XX ... Consent provided Web 5/22/2015 Closed with explanation Yes No 1389659 71.77 64.46 60.27
14590 1/11/2016 Mortgage Conventional fixed mortgage Application, originator, mortgage broker NaN Approximately 18 months ago I restructured my ... NaN Nationstar Mortgage NJ 080XX ... Consent provided Web 1/11/2016 Closed with explanation Yes No 1735152 44638.37 191273.62 55133.04
112590 10/11/2016 Credit card NaN Closing/Cancelling account NaN I received a letter regarding credit card inac... NaN TD Bank US Holding Company CA 906XX ... Consent provided Web 10/11/2016 Closed with explanation Yes Yes 2155781 236.99 199.45 138.43
3466 6/17/2015 Debt collection Other (i.e. phone, health club, etc.) Cont'd attempts collect debt not owed Debt is not mine Enhanced Recovery Corporation for XXXX XXXX XX... Company believes it acted appropriately as aut... ERC CA 917XX ... Consent provided Web 6/17/2015 Closed with non-monetary relief Yes No 1425626 65.32 64.94 49.90
4341 6/10/2015 Debt collection Credit card Communication tactics Threatened to take legal action XXXX XXXX called several times from an unknown... NaN National Check Resolution Inc. PA 190XX ... Consent provided Web 6/16/2015 Closed Yes No 1413976 79.73 60.51 44.94
69239 11/20/2015 Mortgage Conventional adjustable mortgage (ARM) Application, originator, mortgage broker NaN DITECH is offering a misleading refinance prop... NaN Ditech Financial LLC NV 891XX ... Consent provided Web 11/20/2015 Closed with explanation Yes Yes 1665197 170679.21 174707.61 -14251.51
44343 7/29/2015 Credit card NaN Credit line increase/decrease NaN In year XXXX/XXXX/XXXX Citi reports to XXXX a ... Company chooses not to provide a public response Citibank CA 945XX ... Consent provided Web 7/30/2015 Closed with explanation Yes Yes 1495507 174.39 231.40 173.63
52510 12/16/2015 Credit reporting NaN Incorrect information on credit report Account status Experian continue to maintain inaccurate accou... Company chooses not to provide a public response Experian TX 782XX ... Consent provided Web 12/16/2015 Closed with explanation Yes No 1706414 5.05 1.67 1.86
101623 8/11/2016 Consumer Loan Vehicle loan Managing the loan or lease NaN I obtained an auto loan back in 2015. I set pa... Company has responded to the consumer and the ... Wells Fargo & Company TX 751XX ... Consent provided Web 8/11/2016 Closed with explanation No No 2059563 5559.18 5074.81 3227.94
78058 4/29/2016 Debt collection Other (i.e. phone, health club, etc.) Cont'd attempts collect debt not owed Debt was paid I payed off my debt but its still on my credit... Company has responded to the consumer and the ... FAIR COLLECTIONS & OUTSOURCING, INC. NC 282XX ... Consent provided Web 4/29/2016 Closed with explanation Yes No 1903062 68.36 80.42 46.21
32940 12/25/2015 Consumer Loan Vehicle loan Managing the loan or lease NaN I have been paying my auto loan for over two y... Company chooses not to provide a public response Bank of America FL 336XX ... Consent provided Web 12/25/2015 Closed with non-monetary relief Yes No 1716975 5168.42 3333.21 1533.45
88429 6/30/2016 Consumer Loan Installment loan Problems when you are unable to pay NaN In XXXX of XXXX I took out an installment loan... NaN ACE Cash Express Inc. TX 786XX ... Consent provided Web 6/30/2016 Closed with explanation Yes No 1992706 4650.48 3231.60 2721.77
11078 11/14/2015 Debt collection Credit card Communication tactics Threatened to take legal action The company Pressler and Pressler has been har... Company believes it acted appropriately as aut... Pressler & Pressler, LLP NY 148XX ... Consent provided Web 11/14/2015 Closed with explanation Yes Yes 1655557 92.74 71.34 45.02
15034 2/15/2016 Debt collection Other (i.e. phone, health club, etc.) Taking/threatening an illegal action Seized/Attempted to seize property XXXX XXXX send me a renewal policy XX/XX/XXXX.... Company chooses not to provide a public response The Receivable Management Services Corporation CA 926XX ... Consent provided Web 2/15/2016 Closed with non-monetary relief Yes No 1787571 69.39 72.54 19.87
75981 4/20/2016 Debt collection I do not know Cont'd attempts collect debt not owed Debt is not mine For more than a month I have been receiving au... NaN Caine & Weiner Co. Inc. PA 180XX ... Consent provided Web 4/20/2016 Closed with explanation Yes No 1888345 70.74 77.74 45.29
76750 4/29/2016 Consumer Loan Vehicle loan Problems when you are unable to pay NaN Honda Financial has not lived up to their prom... NaN American Honda Finance Corporation MI 483XX ... Consent provided Web 4/29/2016 Closed with monetary relief Yes No 1901049 6549.81 3721.72 2810.62
112094 11/18/2016 Bank account or service Checking account Account opening, closing, or management NaN In XX/XX/2016, request to convert my CitiGold ... Company has responded to the consumer and the ... Citibank CA 917XX ... Consent provided Web 11/18/2016 Closed with monetary relief Yes NaN 2215118 80.64 58.40 52.81
14446 1/14/2016 Mortgage Conventional adjustable mortgage (ARM) Credit decision / Underwriting NaN We are in disagreement with Bank of America Ho... Company chooses not to provide a public response Bank of America FL 328XX ... Consent provided Web 1/14/2016 Closed with explanation Yes No 1743600 171992.81 113252.22 48612.34
49396 10/30/2015 Credit reporting NaN Incorrect information on credit report Information is not mine does not belong to me please remove medical XX... Company chooses not to provide a public response TransUnion Intermediate Holdings, Inc. MO 631XX ... Consent provided Web 10/30/2015 Closed with explanation Yes No 1632456 4.06 7.74 3.52
91923 6/15/2016 Credit card NaN Billing statement NaN On XXXX XXXX, 2015, I purchased XXXX shirts at... Company has responded to the consumer and the ... Citibank DC 200XX ... Consent provided Web 6/15/2016 Closed with monetary relief Yes No 1970921 330.00 162.95 164.15
4139 6/24/2015 Credit card NaN Sale of account NaN For years, I have had a XXXX Rewards Program c... NaN First National Bank of Omaha MA 014XX ... Consent provided Web 6/29/2015 Closed with explanation Yes No 1435820 255.58 146.27 169.15
113444 10/13/2016 Mortgage Other mortgage Loan servicing, payments, escrow account NaN PMI should automatically cancel when loan is u... NaN Ditech Financial LLC CT 064XX ... Consent provided Web 10/13/2016 Closed with explanation Yes Yes 2158427 165395.04 107284.80 94765.64
83010 5/8/2016 Credit reporting NaN Incorrect information on credit report Account status My credit score randomly dropped XXXX points a... Company has responded to the consumer and the ... TransUnion Intermediate Holdings, Inc. VA 231XX ... Consent provided Web 5/8/2016 Closed with non-monetary relief Yes No 1915066 6.60 3.06 3.30
116097 9/17/2016 Money transfers Domestic (US) money transfer Fraud or scam NaN I made a purchase on XXXX XXXX which involved ... NaN MoneyGram NM 875XX ... Consent provided Web 9/21/2016 Closed with explanation Yes No 2117373 73.49 53.97 57.80
17115 3/7/2016 Payday loan Payday loan Received a loan I didn't apply for Received a loan I didn't apply for XX/XX/2012 my apartment was robbed. They stole... NaN Trident Asset Management, L.L.C. OH 431XX ... Consent provided Web 3/8/2016 Closed with non-monetary relief No No 1820036 28.78 20.60 9.13
55126 3/4/2016 Mortgage Conventional fixed mortgage Loan servicing, payments, escrow account NaN My mortgage payment was {$670.00} on both my G... NaN Fifth Third Financial Corporation TN 379XX ... Consent provided Web 3/4/2016 Closed with explanation Yes No 1817095 133336.58 111084.38 57535.22
22600 6/11/2015 Consumer Loan Vehicle loan Managing the loan or lease NaN My loan was reported to the credit bureau as p... NaN Ford Motor Credit Company NE 680XX ... Consent provided Web 6/11/2015 Closed with explanation Yes No 1416569 5747.63 3990.35 3642.86
29919 10/16/2015 Mortgage VA mortgage Loan servicing, payments, escrow account NaN Last week I began receiving collections calls ... NaN Nationstar Mortgage AL 362XX ... Consent provided Web 10/16/2015 Closed with explanation Yes No 1610000 107135.12 78893.44 99686.79
9048 9/10/2015 Debt collection Non-federal student loan Cont'd attempts collect debt not owed Debt was paid This alleged debt is stale dated as it occurre... Company chooses not to provide a public response Windham Professionals CA 913XX ... Consent provided Web 9/17/2015 Closed with non-monetary relief Yes Yes 1559517 69.85 44.48 49.15
49765 11/5/2015 Debt collection Medical Disclosure verification of debt Not given enough info to verify debt I have been negotiating in good faith with XXX... NaN Peerless Credit Services Inc PA 173XX ... Consent provided Web 11/5/2015 Closed with explanation No No 1642296 61.77 76.28 59.00
54778 2/23/2016 Credit reporting NaN Incorrect information on credit report Account status There are XXXX XXXX Mortgage accounts listed o... NaN Equifax TX 782XX ... Consent provided Web 2/23/2016 Closed with explanation Yes No 1801201 5.88 7.85 5.52
53707 1/27/2016 Mortgage Conventional fixed mortgage Loan servicing, payments, escrow account NaN I have been experiencing on going problems wit... Company chooses not to provide a public response Specialized Loan Servicing LLC PA 183XX ... Consent provided Web 1/27/2016 Closed with explanation Yes No 1759217 166764.66 50642.52 -12258.46
32329 12/11/2015 Mortgage Home equity loan or line of credit Application, originator, mortgage broker NaN I am writing to you under a Power of Attorney ... Company chooses not to provide a public response Bank of America WA 985XX ... Consent provided Web 12/11/2015 Closed with explanation Yes Yes 1693608 114771.64 85360.80 128595.11
65155 8/13/2015 Debt collection I do not know Taking/threatening an illegal action Sued where didn't live/sign for debt M & S Recovery Solutions contact me staring th... Company believes complaint represents an oppor... M&S Recovery Solutions DC 200XX ... Consent provided Web 8/27/2015 Closed with explanation Yes No 1518453 65.72 68.07 38.27
1995 5/19/2015 Debt collection I do not know Cont'd attempts collect debt not owed Debt is not mine Reached out to them however, never got a respo... Company chooses not to provide a public response FC HoldCo LLC MD 207XX ... Consent provided Web 5/21/2015 Closed with non-monetary relief Yes No 1383714 94.06 61.47 55.02
7118 8/7/2015 Credit card NaN Delinquent account NaN I have an account with Spencer Savigs Bank whi... Company chooses not to provide a public response U.S. Bancorp NJ 088XX ... Consent provided Web 8/7/2015 Closed with explanation Yes No 1507931 283.18 140.50 133.39
7043 8/6/2015 Credit reporting NaN Incorrect information on credit report Public record FICO ( R ) Score XXXXPowered by ExperianXXXXCr... Company chooses not to provide a public response Experian MD 207XX ... Consent provided Web 8/6/2015 Closed with explanation Yes No 1507200 5.93 1.31 3.35
74576 3/16/2016 Credit card NaN Billing disputes NaN A charge of XXXX was placed against my card fo... Company has responded to the consumer and the ... U.S. Bancorp DC 200XX ... Consent provided Web 3/16/2016 Closed with explanation Yes No 1835977 143.51 235.57 123.60
114562 9/4/2016 Credit reporting NaN Credit reporting company's investigation Problem with statement of dispute Experian unilaterally decided a compliant did ... Company has responded to the consumer and the ... Experian TX 782XX ... Consent provided Web 9/4/2016 Closed with non-monetary relief Yes No 2095685 5.18 2.27 3.28
75737 4/13/2016 Mortgage Conventional fixed mortgage Loan modification,collection,foreclosure NaN I currently have Rushmore, ore Loan Servicing ... NaN Rushmore Loan Management Services LLC MD 212XX ... Consent provided Web 4/13/2016 Closed with explanation Yes No 1878188 94102.06 102013.77 51307.76
94258 8/3/2016 Mortgage Conventional fixed mortgage Loan modification,collection,foreclosure NaN This is the second time we file a complaint ag... Company has responded to the consumer and the ... U.S. Bancorp TX 782XX ... Consent provided Web 8/3/2016 Closed with explanation Yes Yes 2044294 128568.00 116383.16 108854.49
71662 12/21/2015 Credit reporting NaN Incorrect information on credit report Information is not mine I have the credit bureaus to remove the citize... Company chooses not to provide a public response Experian NC 282XX ... Consent provided Web 12/21/2015 Closed with non-monetary relief Yes No 1711989 5.06 8.32 -1.29
7335 8/21/2015 Mortgage Conventional adjustable mortgage (ARM) Loan modification,collection,foreclosure NaN Hi my name is XXXX XXXX XXXX I am with.Sps ser... Company believes it acted appropriately as aut... Select Portfolio Servicing, Inc DC 200XX ... Consent provided Web 8/21/2015 Closed with explanation Yes No 1529782 213047.44 100951.42 168671.19

50 rows × 21 columns


In [11]:
# 8. Get the dataframe 'df' column names

df.columns


Out[11]:
Index(['Date received', 'Product', 'Sub-product', 'Issue', 'Sub-issue',
       'Consumer complaint narrative', 'Company public response', 'Company',
       'State', 'ZIP code', 'Tags', 'Consumer consent provided?',
       'Submitted via', 'Date sent to company', 'Company response to consumer',
       'Timely response?', 'Consumer disputed?', 'Complaint ID',
       'Consumer Claim', 'Company Offer', 'Amount Received'],
      dtype='object')

In [12]:
# 9. Assign the dataframe's index to a variable called 'index'. Get the index values.

index = df.index
index


Out[12]:
RangeIndex(start=0, stop=125680, step=1)

In [13]:
# 10. Create a pandas.Series object with the values [1, 2, 3, 4, 5]. Assign it to varable 's'.

s = pd.Series([1, 2, 3, 4, 5])
s


Out[13]:
0    1
1    2
2    3
3    4
4    5
dtype: int64

In [14]:
# 11. Get the mean, median, amd max of series 's'.

mean = s.mean()
med = s.median()
max_ = s.max()
print(mean, med, max_)


3.0 3.0 5

In [16]:
# 12. Use series.plot() method to graph your series after import matplotlib; %matplotlib inline

import matplotlib

matplotlib.style.use('fivethirtyeight')

%matplotlib inline

s.plot()


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0xd6cff60>

In [17]:
# 13. Add multiply each item in you df by 5 and assign to 's2'. Then use the cumsum() method on 's2'.

s2 = s * 5

s2.cumsum()


Out[17]:
0     5
1    15
2    30
3    50
4    75
dtype: int64

In [18]:
# 14. Create a function that adds 2 to a number and prints it. series.map() it.

def my_function(x):
    x = x + 2 # x += 2
    print(x)
    return x

s.map(my_function);


3
4
5
6
7

In [19]:
# 15. Translate your series into raw numpy values.

s.values


Out[19]:
array([1, 2, 3, 4, 5], dtype=int64)

In [20]:
# 16. Use the value_counts() method of the series to see the value counts.

s.value_counts()


Out[20]:
5    1
4    1
3    1
2    1
1    1
dtype: int64

In [21]:
# 17. Write your data to disk using df.to_csv()

df.to_csv('data/new_location.csv')

In [ ]:
# 18. Export this notebook using the file menu above.

# This is done via the menu.

Next Up: Pandas Selection