What is a CSV File?
A CSV is a (comma separated values) file in which data is in the form of a tabular. The extension of the CSV file is .csv. This csv file is mostly used in the data analytics. Apart from the data analytics, the CSV file also used in the e-commerce application because it’s very easy to handle in all different types of programming languages.
We can convert the CSV to different data structures like a list, a list of tuples and a list of dictionaries. We can also save the CSV without the header or with the header as a list, and for that we can use some machine learning libraries like Pandas.
Example_1: Convert the CSV to List in Python
The below is a CSV sample file which will be used to convert into a list.
"JAN", 340, 360, 417
"FEB", 318, 342, 391
"MAR", 362, 406, 419
"APR", 348, 396, 461
"MAY", 363, 420, 472
"JUN", 435, 472, 535
"JUL", 491, 548, 622
"AUG", 505, 559, 606
"SEP", 404, 463, 508
"OCT", 359, 407, 461
"NOV", 310, 362, 390
"DEC", 337, 405, 432
with open('sample.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
list_of_csv = list(csv_reader)
print(list_of_csv)
Output:
Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. Then we pass the read_obj to the csv.reader() method while creating an object to read the CSV file. Then we convert explicitly the CSV read data into a list using type cast.
Line 6: The output above shows that our CSV data is now successfully converted into the list.
Example_2: Using Pandas to Read CSV List
In this example, we are going to use the Pandas library to read the CSV file and convert them into a list. The CSV file is same which we have used in the example_1 (sample.csv).
df = pd.read_csv('sample.csv', delimiter=',')
list_of_csv = [list(row) for row in df.values]
print(list_of_csv)
Output:
Line 1: We import the Pandas module as pd.
Line 2 to 3: We read the CSV file using the Pandas library read_csv and converted it into a dataframe (df). Then, we convert each row into a list and assign the result to the list_of_csv variable.
Line 4: The output above shows that our CSV data is now successfully converted into the list.
Example_3: Convert the CSV File Data into a List of Tuples
In this example, we are going to convert the CSV file data into a list of tuples. The CSV file is same which we have used in the example_1 (sample.csv).
with open('sample.csv', 'r') as read_obj:
csv_reader = csv.reader(read_obj)
list_of_csv = list(map(tuple, csv_reader))
print(list_of_csv)
Output:
Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. We pass the read_obj to the csv.reader() method while creating an object to read the csv file. Then, we convert each row of the CSV into a tuple using a map function and at last convert the whole data into a list.
Line 5: The output above shows that our CSV data is now successfully converted into a list of tuples.
Example_4: Convert the CSV file data into a list of dictionaries
In this example, we are going to convert the CSV file data into a list of dictionaries. The CSV file is same which we have used in the example_1 (sample.csv).
with open('sample.csv', 'r') as read_obj:
dict_reader = csv.DictReader(read_obj)
list_of_dict = list(dict_reader)
print(list_of_dict)
Output:
Line 1: We import the CSV module.
Line 2 to 4: We open the sample.csv file in the read mode ‘r’. Then, we pass the read_obj to the
csv.DictReader method while creating an object to read the csv file. The csv.DictReader automatically converts each row into a dictionary. And then we convert the whole results into a list.
Line 6: The output above shows that our CSV data is now successfully converted into a list of dictionaries.
Example_5: Using the Pandas to Convert the CSV File Data into a List with the Header
In this example, we are going to use the Pandas library to read the csv file and convert them into a list along with header. The CSV file is same which we have used in the example_1 (sample.csv).
df = pd.read_csv('sample.csv', delimiter=',')
list_of_csv = [list(row) for row in df.values]
list_of_csv.insert(0, df.columns.to_list())
print(list_of_csv)
Output:
Line 1: We import the Pandas module as pd.
Line 2 to 4: We read the csv using the Pandas library read_csv and converted it into a dataframe (df). Then we convert each row into a list and assign the result to the list_of_csv variable. Now, in the next line, we are adding one list item at position 0 of the list_of_csv (list variable). This list item is the name of columns of the CSV file data.
Line 5: The output above shows that our CSV data is now successfully converted into the list and the first list value is the name of columns (header).
Conclusion
In this blog, we have learnt about how to convert the csv file data into a list. We have seen all different methods of list data structure like tuples, dictionaries. We have also seen the same method with the Pandas library. Then we have also seen how to add the header of the CSV into the list.