-1

I downloaded a list of data from here: https://catalog.data.gov/dataset/fdic-failed-bank-list

and it's a pretty standard dataset:

Bank Name,City,ST,CERT,Acquiring Institution,Closing Date,Updated Date
Proficio Bank,Cottonwood Heights,UT,35495,Cache Valley Bank,3-Mar-17,8-Mar-17
Seaway Bank and Trust Company,Chicago,IL,19328,State Bank of Texas,27-Jan-17,17-Feb-17
Harvest Community Bank,Pennsville,NJ,34951,First-Citizens Bank & Trust Company,13-Jan-17,17-Feb-17
Allied Bank,Mulberry,AR,91,Today's Bank,23-Sep-16,17-Nov-16

however, the code I used to put each column into a list came out a lot more verbose than I think It needs to be:

import csv

a = []
b = []
c = []
d = []
e = []
f = []
g = []

with open('banklist.csv', 'rb') as cf:
     bl = csv.reader(cf, delimiter=',')
     for row in bl:
          a.append(row[0])
          b.append(row[1])
          c.append(row[2])
          d.append(row[3])
          e.append(row[4])
          f.append(row[5])
          g.append(row[6])


print a
print b
print c
print d
print e
print f
print g

There has to be a way to make this simpler.

Jason Ayes
  • 19
  • 5

1 Answers1

0

Yes, there is a better way: use pandas DataFrames. Each column in the DataFrame is a separate list:

df = pd.read_csv('your_file.csv')
# Column extraction example:
df['City'].values
# array(['Cottonwood Heights', 'Chicago', 'Pennsville', 'Mulberry'], dtype=object)

If you really want a Python list (no need for this - you can process data in Pandas!), coerce an array to a list:

df['City'].values.tolist()
# ['Cottonwood Heights', 'Chicago', 'Pennsville', 'Mulberry']
DYZ
  • 55,249
  • 10
  • 64
  • 93