I have a csv file that contains data like this with a lot of rows:
2017, UNC (1), Gonzaga (1), Oregon (3), South Carolina (7)
2016, Villanova, (2), UNC (1), Oklahoma (2), Syracuse (10)
I am trying to import the file and make each column into its own list in the easiest way possible in python.
This will give you a list of columns, each column being a list of items.
import pandas as pd
def get_columns(csv_file):
df = pd.read_csv(your.csv)
columns = []
for column in df:
columns += column
return columns
You can use the built in csv
module standard with every python installation.
import csv
header1 = []
header2 = []
# so on
with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile, fieldnames=['header1', 'header2', 'header3', 'header4', 'header5'])
for row in reader:
header1.append(row['header1'])
header2.append(row['header2'])
# so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.