How to Import CSV to List Python

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.

"Month", "1958", "1959", "1960"

"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
import csv

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:

[['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]]

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).

import pandas as pd

df = pd.read_csv('sample.csv', delimiter=',')

list_of_csv = [list(row) for row in df.values]

print(list_of_csv)

Output:

[['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]]

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).

import 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:

[('Month', ' "1958"', ' "1959"', ' "1960"'), ('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')]

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).

import 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:

[{'Month': 'JAN', ' "1958"': '  340', ' "1959"': '  360', ' "1960"': '  417'}, {'Month': 'FEB', ' "1958"': '  318', ' "1959"': '  342', ' "1960"': '  391'}, {'Month': 'MAR', ' "1958"': '  362', ' "1959"': '  406', ' "1960"': '  419'}, {'Month': 'APR', ' "1958"': '  348', ' "1959"': '  396', ' "1960"': '  461'}, {'Month': 'MAY', ' "1958"': '  363', ' "1959"': '  420', ' "1960"': '  472'}, {'Month': 'JUN', ' "1958"': '  435', ' "1959"': '  472', ' "1960"': '  535'}, {'Month': 'JUL', ' "1958"': '  491', ' "1959"': '  548', ' "1960"': '  622'}, {'Month': 'AUG', ' "1958"': '  505', ' "1959"': '  559', ' "1960"': '  606'}, {'Month': 'SEP', ' "1958"': '  404', ' "1959"': '  463', ' "1960"': '  508'}, {'Month': 'OCT', ' "1958"': '  359', ' "1959"': '  407', ' "1960"': '  461'}, {'Month': 'NOV', ' "1958"': '  310', ' "1959"': '  362', ' "1960"': '  390'}, {'Month': 'DEC', ' "1958"': '  337', ' "1959"': '  405', ' "1960"': '  432'}]

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).

import pandas as pd

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:

[['Month', ' "1958"', ' "1959"', ' "1960"'], ['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]]

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.



from https://ift.tt/3HKSJOv

Post a Comment

0 Comments